krahets/hello-algo · error · Error
Illegal Argument Exception
Error message
Illegal Argument Exception
What it means
Thrown by GraphAdjacencyList.addEdge when the call would create an invalid edge. The method refuses to link two vertices unless both already live in the adjacency list and are distinct, mirroring the simple-undirected-graph invariant the class models. Rejected cases: a vertex not previously added via addVertex, or a self-loop (vet1 === vet2).
Source
Thrown at en/codes/typescript/chapter_graph/graph_adjacency_list.ts:37
this.addVertex(edge[0]);
this.addVertex(edge[1]);
this.addEdge(edge[0], edge[1]);
}
}
/* Get the number of vertices */
size(): number {
return this.adjList.size;
}
/* Add edge */
addEdge(vet1: Vertex, vet2: Vertex): void {
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: Vertex, vet2: Vertex): void {
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
- Call addVertex for both endpoints before addEdge.
- Reject or skip self-loops upstream before calling addEdge.
- Keep and reuse the exact Vertex object references returned/added to the graph instead of reconstructing them.
- If value-based identity is needed, switch the adjList Map key to a serializable id and key vertices by id.
Example fix
// before const a = new Vertex(1), b = new Vertex(2); graph.addEdge(a, b); // throws if not added // after graph.addVertex(a); graph.addVertex(b); graph.addEdge(a, b);
Defensive patterns
Strategy: validation
Validate before calling
function canAddEdge(g, v1, v2) {
return g.adjList.has(v1) && g.adjList.has(v2) && v1 !== v2;
}
// usage
if (canAddEdge(graph, a, b)) graph.addEdge(a, b); Type guard
function isRegisteredVertex(g, v) {
return v instanceof Vertex && g.adjList.has(v);
} Try / catch
try { graph.addEdge(a, b); } catch (e) {
if (/Illegal Argument/.test(e.message)) { /* log/skip */ }
else throw e;
} Prevention
- Register every vertex with addVertex before wiring edges.
- Keep a single pool of Vertex references; never reconstruct equivalents.
- Filter self-loops (v1 === v2) in your input pipeline.
When it happens
Trigger: Calling addEdge(a, b) before addVertex(a) or addVertex(b); passing the same Vertex instance for both arguments (self-loop); passing a Vertex object that is structurally equal but not reference-equal to the one stored (Map keys are by reference).
Common situations: Building a graph from edges parsed before nodes are registered; reusing deserialized Vertex objects whose identity differs from the in-graph instances; assuming Vertex equality is by value rather than by reference.
Related errors
- Index Out Of Bounds Exception
- Illegal Argument Exception
- Index Out Of Bounds Exception
- Heap is empty.
- The Deque Is Empty.
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/55280a1a198a4328.
Report an issue: GitHub.