krahets/hello-algo · error · Error
Illegal Argument Exception
Error message
Illegal Argument Exception
What it means
Thrown by addEdge(vet1, vet2) on a graph represented as an adjacency list (Map of vertex -> neighbor array). The method refuses the operation if either vertex is absent from adjList, or if vet1 === vet2 (self-loops are disallowed in this undirected graph model). It then pushes each vertex into the other's neighbor list, so a missing vertex would cause an undefined.push() crash without the guard.
Source
Thrown at codes/javascript/chapter_graph/graph_adjacency_list.js:37
this.addVertex(edge[0]);
this.addVertex(edge[1]);
this.addEdge(edge[0], edge[1]);
}
}
/* 获取顶点数量 */
size() {
return this.adjList.size;
}
/* 添加边 */
addEdge(vet1, vet2) {
if (
!this.adjList.has(vet1) ||
!this.adjList.has(vet2) ||
vet1 === vet2
) {
throw new Error('Illegal Argument Exception');
}
// 添加边 vet1 - vet2
this.adjList.get(vet1).push(vet2);
this.adjList.get(vet2).push(vet1);
}
/* 删除边 */
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');
}
// 删除边 vet1 - vet2
this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);View on GitHub (pinned to 69932aed18)
Solutions
- Ensure both endpoints are added first: if (!g.adjList.has(a)) g.addVertex(a); repeat for b; then g.addEdge(a,b).
- Do not pass the same reference for both args; if self-loops are needed, extend the class rather than bypassing the check.
- Use the exact object references returned/stored by addVertex — Map key equality is by reference for objects.
- Validate input data before constructing the graph so no orphan edges exist.
Example fix
// before
const a = { val: 1 }, b = { val: 2 };
g.addEdge(a, b); // throws if a/b not added
// after
g.addVertex(a); g.addVertex(b);
g.addEdge(a, b); Defensive patterns
Strategy: validation
Validate before calling
function safeAddEdge(g, a, b) {
if (!g.adjList.has(a)) g.addVertex(a);
if (!g.adjList.has(b)) g.addVertex(b);
if (a !== b) g.addEdge(a, b);
} Type guard
function isRegisteredVertex(g, v) {
return g.adjList.has(v);
} Try / catch
try {
g.addEdge(a, b);
} catch (e) {
if (e instanceof Error && e.message === 'Illegal Argument Exception') {
// ensure vertices exist and a !== b, then retry or skip
} else throw e;
} Prevention
- Always addVertex() both endpoints before addEdge().
- Pass the exact object references stored as Map keys (reference equality).
- Reject self-loops upstream if your model forbids them.
- Validate edge lists from external data for orphan endpoints before loading.
When it happens
Trigger: Calling addEdge before both vertices were addVertex()'d; passing the same object reference for both arguments (self-loop); passing a primitive that was never registered; passing a vertex from a different GraphAdjList instance.
Common situations: Building edges before nodes; assuming vertices auto-create on first edge; reusing object literals whose reference identity differs from the stored vertex (Map keys compare by reference); loading graph data where some node rows are missing.
Related errors
- Illegal Argument Exception
- Illegal Argument Exception
- Illegal Argument Exception
- Index Out Of Bounds Exception
- Illegal Argument Exception
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/3d7a3a592942427d.
Report an issue: GitHub.