{"record":{"id":"0db328c9204d9f65","repo":"krahets/hello-algo","slug":"illegal-argument-exception-0db328","errorCode":null,"errorMessage":"Illegal Argument Exception","messagePattern":"Illegal Argument Exception","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ja/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/ja/codes/typescript/chapter_graph/graph_adjacency_list.ts#L19-L55","documentation":"Thrown by the TypeScript adjacency-list graph's addEdge when either endpoint is not a registered Vertex (adjList.has fails), or when both arguments are the same reference (self-loop disallowed). The Vertex type is enforced at compile time, but existence in the Map and distinctness are runtime checks. The guard protects the subsequent .push calls from undefined neighbor lists.","triggerScenarios":"Calling addEdge(vet1, vet2) where vet1/vet2 were not added via addVertex; passing the same Vertex reference twice; passing a Vertex equal-by-val but distinct-by-reference from the stored key (Map uses reference equality).","commonSituations":"TypeScript guarantees the type but not graph membership; forgetting addVertex; reconstructing a Vertex from its val field instead of retrieving the stored instance; building edges before vertices.","solutions":["Call addVertex for both endpoints and pass back the exact stored references.","Maintain a Map<number, Vertex> keyed by val to always retrieve the canonical instance.","Guard against vet1 === vet2 before calling addEdge.","Expose a hasVertex(vet) helper and check it at the call site."],"exampleFix":"// before\ngraph.addEdge(new Vertex(1), v2);  // new instance not in graph -> throws\n\n// after\nconst v1 = vertexById.get(1)!;  // retrieve stored reference\ngraph.addEdge(v1, v2);","handlingStrategy":"type-guard","validationCode":"function safeAddEdge(graph: GraphAdjacencyList, vet1: Vertex, vet2: Vertex): boolean {\n  if (vet1 === vet2) return false;\n  if (!graph.adjList.has(vet1) || !graph.adjList.has(vet2)) return false;\n  graph.addEdge(vet1, vet2);\n  return true;\n}","typeGuard":"const isRegisteredVertex = (graph: GraphAdjacencyList, v: Vertex): boolean =>\n  graph.adjList.has(v);","tryCatchPattern":"try {\n  graph.addEdge(vet1, vet2);\n} catch (e) {\n  if (e instanceof Error && e.message === 'Illegal Argument Exception') {\n    // missing vertex or self-loop\n  } else throw e;\n}","preventionTips":["TypeScript checks Vertex shape but not graph membership — validate at runtime.","Maintain a Map<number, Vertex> by val to retrieve canonical references.","Reject self-loops (vet1 === vet2) before calling."],"tags":["graph","adjacency-list","typescript","validation","precondition"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}