{"record":{"id":"afec66320b8ce99f","repo":"krahets/hello-algo","slug":"illegal-argument-exception-afec66","errorCode":null,"errorMessage":"Illegal Argument Exception","messagePattern":"Illegal Argument Exception","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"zh-hant/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/zh-hant/codes/javascript/chapter_graph/graph_adjacency_list.js#L19-L55","documentation":"Thrown by addEdge(vet1, vet2) in an adjacency-list graph when either vertex is absent from the Map, or when vet1 === vet2 (self-loops disallowed). Map keys use reference identity (SameValueZero), so a vertex object with the same val but a different reference is treated as absent. The guard prevents calling .push on an undefined adjacency list.","triggerScenarios":"Calling addEdge with a Vertex not previously added via the constructor or addVertex; passing two different references that represent the same logical vertex; attempting addEdge(v, v) to form a self-loop.","commonSituations":"Deserializing vertices from storage and passing new instances to addEdge; aliasing bugs where two Vertex objects share a val but differ in reference; graph algorithms that try self-loops.","solutions":["Add both vertices via addVertex (or the constructor edges) before calling addEdge, using the same object references.","Guard the call: if (adjList.has(vet1) && adjList.has(vet2) && vet1 !== vet2) graph.addEdge(vet1, vet2);","If val-based identity is needed, key the Map by a primitive id instead of the Vertex object.","Wrap in try/catch when adding edges from untrusted sources."],"exampleFix":"// before\ngraph.addEdge(new Vertex(1), new Vertex(2)); // refs not stored -> throws\n\n// after\nconst v1 = new Vertex(1), v2 = new Vertex(2);\ngraph.addVertex(v1);\ngraph.addVertex(v2);\ngraph.addEdge(v1, v2);","handlingStrategy":"validation","validationCode":"if (\n    graph.adjList.has(vet1) &&\n    graph.adjList.has(vet2) &&\n    vet1 !== vet2\n) {\n    graph.addEdge(vet1, vet2);\n}","typeGuard":"function canAddEdge(graph, vet1, vet2) {\n    return (\n        graph.adjList.has(vet1) &&\n        graph.adjList.has(vet2) &&\n        vet1 !== vet2\n    );\n}","tryCatchPattern":"try {\n    graph.addEdge(vet1, vet2);\n} catch (e) {\n    if (e instanceof Error && e.message === 'Illegal Argument Exception') {\n        // vertices missing or self-loop; skip or log\n    } else throw e;\n}","preventionTips":["Add both vertices with addVertex using the same references before addEdge.","Never reconstruct Vertex objects from val and expect Map identity to match.","Reject self-loops (vet1 === vet2) in caller logic."],"tags":["graph","adjacency-list","javascript","vertex","reference-equality","self-loop","validation"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}