{"record":{"id":"e08738e592c27d5b","repo":"krahets/hello-algo","slug":"illegal-argument-exception-e08738","errorCode":null,"errorMessage":"Illegal Argument Exception","messagePattern":"Illegal Argument Exception","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ru/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/ru/codes/javascript/chapter_graph/graph_adjacency_list.js#L19-L55","documentation":"Thrown by GraphAdjList.addEdge (JS) when an edge cannot be added. The method validates that both endpoints exist in the adjacency list and are distinct vertices before mutating state. This is the standard precondition guard for undirected-graph edge insertion in the hello-algo educational library.","triggerScenarios":"addEdge(vet1, vet2) is called where vet1 or vet2 was never registered via addVertex (so adjList.has() returns false), or where vet1 === vet2 (a self-loop, which this implementation forbids).","commonSituations":"Forgetting to call addVertex on a vertex before wiring edges; passing the same Vertex object for both args; reusing a vertex reference after it was removed; building a graph from parsed data without pre-populating the vertex set.","solutions":["Ensure every vertex passed to addEdge was first added with addVertex(vet).","Guard the call: if (g.adjList.has(v1) && g.adjList.has(v2) && v1 !== v2) g.addEdge(v1, v2).","If self-loops are legitimately needed, fork the class or relax the vet1 === vet2 check.","When loading a graph from edges, add all distinct vertices in a first pass, then add edges in a second pass."],"exampleFix":"// before\nconst g = new GraphAdjList();\ng.addEdge(v1, v2); // throws if v1/v2 absent\n\n// after\nconst g = new GraphAdjList();\ng.addVertex(v1); g.addVertex(v2);\nif (v1 !== v2) g.addEdge(v1, v2);","handlingStrategy":"validation","validationCode":"// Verify both vertices exist and are distinct before addEdge\nfunction canAddEdge(g, v1, v2) {\n  return g.adjList.has(v1) && g.adjList.has(v2) && v1 !== v2;\n}\nif (canAddEdge(g, v1, v2)) g.addEdge(v1, v2);","typeGuard":"// Vertex is typically { val: number } in hello-algo\nfunction isVertex(v) {\n  return v != null && typeof v === 'object' && 'val' in v;\n}","tryCatchPattern":null,"preventionTips":["Always run addVertex for every vertex before any addEdge.","Build graphs in two passes: all vertices, then all edges.","Never pass the same Vertex reference for both endpoints.","After removeVertex, discard any stale Vertex references still held by callers."],"tags":["graph","javascript","precondition","validation","hello-algo"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}