{"record":{"id":"3d7a3a592942427d","repo":"krahets/hello-algo","slug":"illegal-argument-exception","errorCode":null,"errorMessage":"Illegal Argument Exception","messagePattern":"Illegal Argument Exception","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"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/codes/javascript/chapter_graph/graph_adjacency_list.js#L19-L55","documentation":"Thrown by addEdge(vet1, vet2) on a graph represented as an adjacency list (Map of vertex -> neighbor array). The method refuses the operation if either vertex is absent from adjList, or if vet1 === vet2 (self-loops are disallowed in this undirected graph model). It then pushes each vertex into the other's neighbor list, so a missing vertex would cause an undefined.push() crash without the guard.","triggerScenarios":"Calling addEdge before both vertices were addVertex()'d; passing the same object reference for both arguments (self-loop); passing a primitive that was never registered; passing a vertex from a different GraphAdjList instance.","commonSituations":"Building edges before nodes; assuming vertices auto-create on first edge; reusing object literals whose reference identity differs from the stored vertex (Map keys compare by reference); loading graph data where some node rows are missing.","solutions":["Ensure both endpoints are added first: if (!g.adjList.has(a)) g.addVertex(a); repeat for b; then g.addEdge(a,b).","Do not pass the same reference for both args; if self-loops are needed, extend the class rather than bypassing the check.","Use the exact object references returned/stored by addVertex — Map key equality is by reference for objects.","Validate input data before constructing the graph so no orphan edges exist."],"exampleFix":"// before\nconst a = { val: 1 }, b = { val: 2 };\ng.addEdge(a, b); // throws if a/b not added\n// after\ng.addVertex(a); g.addVertex(b);\ng.addEdge(a, b);","handlingStrategy":"validation","validationCode":"function safeAddEdge(g, a, b) {\n  if (!g.adjList.has(a)) g.addVertex(a);\n  if (!g.adjList.has(b)) g.addVertex(b);\n  if (a !== b) g.addEdge(a, b);\n}","typeGuard":"function isRegisteredVertex(g, v) {\n  return g.adjList.has(v);\n}","tryCatchPattern":"try {\n  g.addEdge(a, b);\n} catch (e) {\n  if (e instanceof Error && e.message === 'Illegal Argument Exception') {\n    // ensure vertices exist and a !== b, then retry or skip\n  } else throw e;\n}","preventionTips":["Always addVertex() both endpoints before addEdge().","Pass the exact object references stored as Map keys (reference equality).","Reject self-loops upstream if your model forbids them.","Validate edge lists from external data for orphan endpoints before loading."],"tags":["graph","illegal-argument","javascript","adjacency-list"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}