krahets/hello-algo · error · Error
Illegal Argument Exception
Error message
Illegal Argument Exception
What it means
Thrown by GraphAdjList.addEdge (JS) when an edge cannot be added. The method validates that both endpoints exist in the adjacency list and are distinct vertices before mutating state. This is the standard precondition guard for undirected-graph edge insertion in the hello-algo educational library.
Source
Thrown at ru/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 every vertex passed to addEdge was first added with addVertex(vet).
- Guard the call: if (g.adjList.has(v1) && g.adjList.has(v2) && v1 !== v2) g.addEdge(v1, v2).
- If self-loops are legitimately needed, fork the class or relax the vet1 === vet2 check.
- When loading a graph from edges, add all distinct vertices in a first pass, then add edges in a second pass.
Example fix
// before const g = new GraphAdjList(); g.addEdge(v1, v2); // throws if v1/v2 absent // after const g = new GraphAdjList(); g.addVertex(v1); g.addVertex(v2); if (v1 !== v2) g.addEdge(v1, v2);
Defensive patterns
Strategy: validation
Validate before calling
// Verify both vertices exist and are distinct before addEdge
function canAddEdge(g, v1, v2) {
return g.adjList.has(v1) && g.adjList.has(v2) && v1 !== v2;
}
if (canAddEdge(g, v1, v2)) g.addEdge(v1, v2); Type guard
// Vertex is typically { val: number } in hello-algo
function isVertex(v) {
return v != null && typeof v === 'object' && 'val' in v;
} Prevention
- Always run addVertex for every vertex before any addEdge.
- Build graphs in two passes: all vertices, then all edges.
- Never pass the same Vertex reference for both endpoints.
- After removeVertex, discard any stale Vertex references still held by callers.
When it happens
Trigger: addEdge(vet1, vet2) is called where vet1 or vet2 was never registered via addVertex (so adjList.has() returns false), or where vet1 === vet2 (a self-loop, which this implementation forbids).
Common situations: Forgetting to call addVertex on a vertex before wiring edges; passing the same Vertex object for both args; reusing a vertex reference after it was removed; building a graph from parsed data without pre-populating the vertex set.
Related errors
- Illegal Argument Exception
- Index Out Of Bounds 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/e08738e592c27d5b.
Report an issue: GitHub.