krahets/hello-algo · error · Error

Illegal Argument Exception

Error message

Illegal Argument Exception

What it means

Thrown by addEdge(vet1, vet2) on a graph backed by an adjacency list. The method requires both vertices to already exist in the adjacency map AND that vet1 !== vet2 (no self-loops). It does not auto-create vertices; you must addVertex() first. The error message 'Illegal Argument Exception' is a generic JS Error (not a typed Java exception).

Source

Thrown at en/codes/javascript/chapter_graph/graph_adjacency_list.js:37

            this.addVertex(edge[0]);
            this.addVertex(edge[1]);
            this.addEdge(edge[0], edge[1]);
        }
    }

    /* Get the number of vertices */
    size() {
        return this.adjList.size;
    }

    /* Add edge */
    addEdge(vet1, vet2) {
        if (
            !this.adjList.has(vet1) ||
            !this.adjList.has(vet2) ||
            vet1 === vet2
        ) {
            throw new Error('Illegal Argument Exception');
        }
        // Add edge vet1 - vet2
        this.adjList.get(vet1).push(vet2);
        this.adjList.get(vet2).push(vet1);
    }

    /* Remove edge */
    removeEdge(vet1, vet2) {
        if (
            !this.adjList.has(vet1) ||
            !this.adjList.has(vet2) ||
            vet1 === vet2 ||
            this.adjList.get(vet1).indexOf(vet2) === -1
        ) {
            throw new Error('Illegal Argument Exception');
        }
        // Remove edge vet1 - vet2
        this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);

View on GitHub (pinned to 69932aed18)

Solutions

  1. Ensure both vertices are added first: graph.addVertex(vet1); graph.addVertex(vet2); graph.addEdge(vet1, vet2);
  2. Before addEdge, verify graph.vertices/adjList contains both: if (!graph.adjList.has(vet1) || !graph.adjList.has(vet2)) addVertex them.
  3. Reject or special-case self-loops before calling addEdge if they are intended.
  4. Keep vertex identity consistent — always pass the same object reference used at addVertex time (Map uses reference equality for objects).

Example fix

// before
graph.addEdge(new Vertex(1), new Vertex(2)); // new instances, not in adjList -> throws

// after
const v1 = new Vertex(1), v2 = new Vertex(2);
graph.addVertex(v1);
graph.addVertex(v2);
graph.addEdge(v1, v2);
Defensive patterns

Strategy: validation

Validate before calling

function canAddEdge(graph, v1, v2) {
  return v1 !== v2 && graph.adjList.has(v1) && graph.adjList.has(v2);
}
if (canAddEdge(graph, vet1, vet2)) graph.addEdge(vet1, vet2);

Type guard

function isKnownVertex(graph, v) {
  return v != null && graph.adjList.has(v);
}

Try / catch

try {
  graph.addEdge(v1, v2);
} catch (e) {
  if (e.message === 'Illegal Argument Exception') { /* missing vertex or self-loop */ }
  else throw e;
}

Prevention

When it happens

Trigger: Calling graph.addEdge(vet1, vet2) when either vertex was never added via addVertex(), when the vertex references are stale/undefined, or when passing the same vertex object for both arguments (self-loop).

Common situations: Adding an edge before creating its endpoints; reusing vertex objects after they were removed via removeVertex; attempting self-loops in an undirected graph model; passing primitive values when vertices are objects (Map key mismatch).

Related errors


AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13). Data as JSON: /api/errors/f1efe6b80f54922d. Report an issue: GitHub.