{"record":{"id":"55280a1a198a4328","repo":"krahets/hello-algo","slug":"illegal-argument-exception-55280a","errorCode":null,"errorMessage":"Illegal Argument Exception","messagePattern":"Illegal Argument Exception","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"en/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    /* Get the number of vertices */\n    size(): number {\n        return this.adjList.size;\n    }\n\n    /* Add edge */\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        // Add edge vet1 - vet2\n        this.adjList.get(vet1).push(vet2);\n        this.adjList.get(vet2).push(vet1);\n    }\n\n    /* Remove edge */\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        // Remove edge 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/en/codes/typescript/chapter_graph/graph_adjacency_list.ts#L19-L55","documentation":"Thrown by GraphAdjacencyList.addEdge when the call would create an invalid edge. The method refuses to link two vertices unless both already live in the adjacency list and are distinct, mirroring the simple-undirected-graph invariant the class models. Rejected cases: a vertex not previously added via addVertex, or a self-loop (vet1 === vet2).","triggerScenarios":"Calling addEdge(a, b) before addVertex(a) or addVertex(b); passing the same Vertex instance for both arguments (self-loop); passing a Vertex object that is structurally equal but not reference-equal to the one stored (Map keys are by reference).","commonSituations":"Building a graph from edges parsed before nodes are registered; reusing deserialized Vertex objects whose identity differs from the in-graph instances; assuming Vertex equality is by value rather than by reference.","solutions":["Call addVertex for both endpoints before addEdge.","Reject or skip self-loops upstream before calling addEdge.","Keep and reuse the exact Vertex object references returned/added to the graph instead of reconstructing them.","If value-based identity is needed, switch the adjList Map key to a serializable id and key vertices by id."],"exampleFix":"// before\nconst a = new Vertex(1), b = new Vertex(2);\ngraph.addEdge(a, b); // throws if not added\n\n// after\ngraph.addVertex(a);\ngraph.addVertex(b);\ngraph.addEdge(a, b);","handlingStrategy":"validation","validationCode":"function canAddEdge(g, v1, v2) {\n  return g.adjList.has(v1) && g.adjList.has(v2) && v1 !== v2;\n}\n// usage\nif (canAddEdge(graph, a, b)) graph.addEdge(a, b);","typeGuard":"function isRegisteredVertex(g, v) {\n  return v instanceof Vertex && g.adjList.has(v);\n}","tryCatchPattern":"try { graph.addEdge(a, b); } catch (e) {\n  if (/Illegal Argument/.test(e.message)) { /* log/skip */ }\n  else throw e;\n}","preventionTips":["Register every vertex with addVertex before wiring edges.","Keep a single pool of Vertex references; never reconstruct equivalents.","Filter self-loops (v1 === v2) in your input pipeline."],"tags":["graph","typescript","validation","guard-clause","reference-equality"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}