{"record":{"id":"fc1cfc36c913e10d","repo":"krahets/hello-algo","slug":"illegal-argument-exception-fc1cfc","errorCode":null,"errorMessage":"Illegal Argument Exception","messagePattern":"Illegal Argument Exception","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ja/codes/javascript/chapter_graph/graph_adjacency_list.js","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() {\n        return this.adjList.size;\n    }\n\n    /* 辺を追加 */\n    addEdge(vet1, vet2) {\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, vet2) {\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/javascript/chapter_graph/graph_adjacency_list.js#L19-L55","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\nconst v = new Vertex(1);\ngraph.addEdge(v, other);  // v was never added -> Illegal Argument Exception\n\n// after\nconst v = new Vertex(1);\ngraph.addVertex(v);\ngraph.addEdge(v, other);","handlingStrategy":"validation","validationCode":"// Before addEdge, confirm both endpoints are registered and distinct.\nfunction safeAddEdge(graph, vet1, vet2) {\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":"// Vertex membership guard (Map uses reference equality).\nconst isRegisteredVertex = (graph, v) => 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    // handle: missing vertex or self-loop\n  } else throw e;\n}","preventionTips":["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."],"tags":["graph","adjacency-list","javascript","validation","precondition"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}