{"record":{"id":"f1efe6b80f54922d","repo":"krahets/hello-algo","slug":"illegal-argument-exception-f1efe6","errorCode":null,"errorMessage":"Illegal Argument Exception","messagePattern":"Illegal Argument Exception","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"en/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    /* Get the number of vertices */\n    size() {\n        return this.adjList.size;\n    }\n\n    /* Add edge */\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        // 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, 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        // 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/javascript/chapter_graph/graph_adjacency_list.js#L19-L55","documentation":"Thrown by addEdge(vet1, vet2) on a graph backed by an adjacency list. The method requires both vertices to already exist in the adjacency map AND that vet1 !== vet2 (no self-loops). It does not auto-create vertices; you must addVertex() first. The error message 'Illegal Argument Exception' is a generic JS Error (not a typed Java exception).","triggerScenarios":"Calling graph.addEdge(vet1, vet2) when either vertex was never added via addVertex(), when the vertex references are stale/undefined, or when passing the same vertex object for both arguments (self-loop).","commonSituations":"Adding an edge before creating its endpoints; reusing vertex objects after they were removed via removeVertex; attempting self-loops in an undirected graph model; passing primitive values when vertices are objects (Map key mismatch).","solutions":["Ensure both vertices are added first: graph.addVertex(vet1); graph.addVertex(vet2); graph.addEdge(vet1, vet2);","Before addEdge, verify graph.vertices/adjList contains both: if (!graph.adjList.has(vet1) || !graph.adjList.has(vet2)) addVertex them.","Reject or special-case self-loops before calling addEdge if they are intended.","Keep vertex identity consistent — always pass the same object reference used at addVertex time (Map uses reference equality for objects)."],"exampleFix":"// before\ngraph.addEdge(new Vertex(1), new Vertex(2)); // new instances, not in adjList -> 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":"function canAddEdge(graph, v1, v2) {\n  return v1 !== v2 && graph.adjList.has(v1) && graph.adjList.has(v2);\n}\nif (canAddEdge(graph, vet1, vet2)) graph.addEdge(vet1, vet2);","typeGuard":"function isKnownVertex(graph, v) {\n  return v != null && graph.adjList.has(v);\n}","tryCatchPattern":"try {\n  graph.addEdge(v1, v2);\n} catch (e) {\n  if (e.message === 'Illegal Argument Exception') { /* missing vertex or self-loop */ }\n  else throw e;\n}","preventionTips":["Always addVertex() for both endpoints before addEdge().","Reuse the exact object references registered via addVertex (Map uses reference equality).","Reject self-loops in your caller if they are invalid for your model."],"tags":["graph","adjacency-list","validation","vertex-lifecycle","javascript"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}