krahets/hello-algo · error · Error
Illegal Argument Exception
Error message
Illegal Argument Exception
What it means
Thrown by the adjacency-list graph's addEdge method when an edge cannot be safely created. The guard rejects three cases: either endpoint vertex is not registered in the Map, or both arguments are the exact same vertex reference (self-loops are disallowed). This is a defensive precondition check so the subsequent adjList.get(...).push(...) calls never receive undefined.
Source
Thrown at ja/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
- Call addVertex(vet1) and addVertex(vet2) for both endpoints before calling addEdge, and pass back the exact references the graph holds.
- Ensure vet1 and vet2 are distinct object references (vet1 !== vet2) before calling addEdge.
- If you construct vertices from numeric values, maintain your own id->Vertex map so you always retrieve the stored reference instead of constructing a duplicate.
- Wrap the call in try/catch only when the inputs are genuinely untrusted.
Example fix
// before const v = new Vertex(1); graph.addEdge(v, other); // v was never added -> Illegal Argument Exception // after const v = new Vertex(1); graph.addVertex(v); graph.addEdge(v, other);
Defensive patterns
Strategy: validation
Validate before calling
// Before addEdge, confirm both endpoints are registered and distinct.
function safeAddEdge(graph, vet1, vet2) {
if (vet1 === vet2) return false;
if (!graph.adjList.has(vet1) || !graph.adjList.has(vet2)) return false;
graph.addEdge(vet1, vet2);
return true;
} Type guard
// Vertex membership guard (Map uses reference equality). const isRegisteredVertex = (graph, v) => graph.adjList.has(v);
Try / catch
try {
graph.addEdge(vet1, vet2);
} catch (e) {
if (e instanceof Error && e.message === 'Illegal Argument Exception') {
// handle: missing vertex or self-loop
} else throw e;
} Prevention
- Always addVertex before addEdge, and pass back the exact stored references.
- Keep a val->Vertex map so you never reconstruct duplicate instances.
- Reject self-loops (vet1 === vet2) at the call site.
When it happens
Trigger: Calling addEdge(vet1, vet2) when vet1 or vet2 was never passed to addVertex (so adjList.has(...) returns false); passing the same object reference for both arguments (vet1 === vet2); passing a freshly-constructed Vertex whose reference differs from the one stored in the graph (Map keys use SameValueZero/reference equality).
Common situations: Building a graph from raw values and forgetting to addVertex first; creating a new Vertex(val) at call time instead of reusing the stored instance; copy-pasting addEdge calls into a loop where one operand is reused as both endpoints.
Related errors
- Illegal Argument Exception
- Illegal Argument Exception
- Illegal Argument Exception
- Illegal Argument Exception
- Illegal Argument Exception
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/fc1cfc36c913e10d.
Report an issue: GitHub.