{"record":{"id":"aea936a5c3654164","repo":"krahets/hello-algo","slug":"illegal-argument-exception-aea936","errorCode":null,"errorMessage":"Illegal Argument Exception","messagePattern":"Illegal Argument Exception","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ru/codes/typescript/chapter_graph/graph_adjacency_list.ts","lineNumber":37,"sourceCode":"            this.addVertex(edge[0]);\n            this.addVertex(edge[1]);\n            this.addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* Получить число вершин */\n    size(): number {\n        return this.adjList.size;\n    }\n\n    /* Добавление ребра */\n    addEdge(vet1: Vertex, vet2: Vertex): void {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // Добавить ребро vet1 - vet2\n        this.adjList.get(vet1).push(vet2);\n        this.adjList.get(vet2).push(vet1);\n    }\n\n    /* Удаление ребра */\n    removeEdge(vet1: Vertex, vet2: Vertex): void {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2 ||\n            this.adjList.get(vet1).indexOf(vet2) === -1\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // Удалить ребро vet1 - vet2\n        this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ru/codes/typescript/chapter_graph/graph_adjacency_list.ts#L19-L55","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ng.addEdge(v1, v2); // throws if absent\n\n// after\n[v1, v2].forEach(v => { if (!g.adjList.has(v)) g.addVertex(v); });\nif (v1 !== v2) g.addEdge(v1, v2);","handlingStrategy":"type-guard","validationCode":"function canAddEdgeTS(g, v1, v2) {\n  return g.adjList.has(v1) && g.adjList.has(v2) && v1 !== v2;\n}\nif (canAddEdgeTS(g, v1, v2)) g.addEdge(v1, v2);","typeGuard":"import { Vertex } from './module';\nfunction isVertex(v: unknown): v is Vertex {\n  return v != null && typeof v === 'object' && 'val' in v;\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["graph","typescript","precondition","validation","hello-algo"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}