krahets/hello-algo · error · Error
Illegal Argument Exception
Error message
Illegal Argument Exception
What it means
Thrown by GraphAdjList.addEdge (TS) — the TypeScript twin of error 120 — when either endpoint Vertex is not in the adjacency list or when vet1 === vet2. TypeScript typing (Vertex) narrows the input type but does not eliminate the runtime membership/self-loop cases, so the guard remains.
Source
Thrown at ru/codes/typescript/chapter_graph/graph_adjacency_list.ts:37
this.addVertex(edge[0]);
this.addVertex(edge[1]);
this.addEdge(edge[0], edge[1]);
}
}
/* Получить число вершин */
size(): number {
return this.adjList.size;
}
/* Добавление ребра */
addEdge(vet1: Vertex, vet2: Vertex): void {
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: 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');
}
// Удалить ребро vet1 - vet2
this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);View on GitHub (pinned to 69932aed18)
Solutions
- Pre-populate all vertices with addVertex before adding any edge.
- Guard at runtime: if (g.adjList.has(v1) && g.adjList.has(v2) && v1 !== v2) g.addEdge(v1, v2).
- Build from an edge list in two passes: vertices first, edges second.
Example fix
// before
g.addEdge(v1, v2); // throws if absent
// after
[v1, v2].forEach(v => { if (!g.adjList.has(v)) g.addVertex(v); });
if (v1 !== v2) g.addEdge(v1, v2); Defensive patterns
Strategy: type-guard
Validate before calling
function canAddEdgeTS(g, v1, v2) {
return g.adjList.has(v1) && g.adjList.has(v2) && v1 !== v2;
}
if (canAddEdgeTS(g, v1, v2)) g.addEdge(v1, v2); Type guard
import { Vertex } from './module';
function isVertex(v: unknown): v is Vertex {
return v != null && typeof v === 'object' && 'val' in v;
} Prevention
- TS types prevent non-Vertex args but not absent/duplicate vertices — validate at runtime.
- Populate all vertices before adding edges.
- Discard Vertex references after removeVertex to avoid stale-endpoint errors.
When it happens
Trigger: addEdge(vet1, vet2) with a Vertex not added via addVertex, or with vet1 === vet2 (same reference). TS type-checking prevents passing non-Vertex values but not absent or duplicated vertices.
Common situations: Building a graph from typed data without a first vertex-population pass; aliasing the same Vertex object for both arguments; reusing a Vertex reference after removeVertex.
Related errors
- Illegal Argument Exception
- Illegal Argument Exception
- Illegal Argument Exception
- Illegal Argument Exception
- Index Out Of Bounds Exception
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/aea936a5c3654164.
Report an issue: GitHub.