{"record":{"id":"4a3b3dffcec012b2","repo":"krahets/hello-algo","slug":"illegal-argument-exception-4a3b3d","errorCode":null,"errorMessage":"Illegal Argument Exception","messagePattern":"Illegal Argument Exception","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"zh-hant/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/zh-hant/codes/typescript/chapter_graph/graph_adjacency_list.ts#L19-L55","documentation":"Thrown by GraphAdjList.addEdge(vet1, vet2) when either vertex is absent from the adjacency list, or when vet1 === vet2 (self-loop). The method then pushes each vertex into the other's neighbor list; the guard prevents pushing into an undefined neighbor array.","triggerScenarios":"Calling addEdge with a vertex not added via addVertex, or passing the same Vertex reference for both endpoints.","commonSituations":"Forgetting to addVertex before connecting edges; using Vertex objects from a different graph; attempting self-loops in an undirected simple graph.","solutions":["Call addVertex for both endpoints before addEdge.","Ensure vet1 and vet2 are distinct references.","Verify graph.adjList.has(vet1) && graph.adjList.has(vet2) before addEdge."],"exampleFix":"// before\ngraph.addEdge(v1, v2); // throws if vertex not registered or v1 === v2\n\n// after\ngraph.addVertex(v1);\ngraph.addVertex(v2);\nif (v1 !== v2) {\n    graph.addEdge(v1, v2);\n}","handlingStrategy":"validation","validationCode":"// Register both vertices and ensure distinct before adding edge\ngraph.addVertex(v1);\ngraph.addVertex(v2);\nif (v1 !== v2) {\n    graph.addEdge(v1, v2);\n}","typeGuard":"function canAddEdge(graph: GraphAdjList, v1: Vertex, v2: Vertex): boolean {\n    return graph.adjList.has(v1) && graph.adjList.has(v2) && v1 !== v2;\n}","tryCatchPattern":"try {\n    graph.addEdge(v1, v2);\n} catch (e) {\n    if (e.message === 'Illegal Argument Exception') {\n        // vertex not registered or self-loop — handle\n    } else throw e;\n}","preventionTips":["Call addVertex for both endpoints before addEdge.","Ensure vet1 and vet2 are distinct references (no self-loops).","Use Vertex objects from the same graph instance only."],"tags":["graph","adjacency-list","typescript","precondition","validation"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}