{"record":{"id":"988f88ef511a9876","repo":"krahets/hello-algo","slug":"index-out-of-bounds-exception-988f88","errorCode":null,"errorMessage":"Index Out Of Bounds Exception","messagePattern":"Index Out Of Bounds Exception","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"en/codes/javascript/chapter_graph/graph_adjacency_matrix.js","lineNumber":52,"sourceCode":"        const n = this.size();\n        // Add the value of the new vertex to the vertex list\n        this.vertices.push(val);\n        // Add a row to the adjacency matrix\n        const newRow = [];\n        for (let j = 0; j < n; j++) {\n            newRow.push(0);\n        }\n        this.adjMat.push(newRow);\n        // Add a column to the adjacency matrix\n        for (const row of this.adjMat) {\n            row.push(0);\n        }\n    }\n\n    /* Remove vertex */\n    removeVertex(index) {\n        if (index >= this.size()) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // Remove the vertex at index from the vertex list\n        this.vertices.splice(index, 1);\n\n        // Remove the row at index from the adjacency matrix\n        this.adjMat.splice(index, 1);\n        // Remove the column at index from the adjacency matrix\n        for (const row of this.adjMat) {\n            row.splice(index, 1);\n        }\n    }\n\n    /* Add edge */\n    // Parameters i, j correspond to the vertices element indices\n    addEdge(i, j) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/javascript/chapter_graph/graph_adjacency_matrix.js#L34-L70","documentation":"Thrown as a RangeError by removeVertex(index) on an adjacency-matrix graph when index >= size() (the number of vertices). Note the guard checks only the upper bound (index >= size) and omits the negative-index check, but the subsequent splice with a negative index would behave unexpectedly. It removes the vertex's row/column from the matrix.","triggerScenarios":"Calling removeVertex(index) with index >= graph.size(), or with a negative index (not explicitly guarded, may cause splice quirks rather than a clean throw). After removals, a previously valid index can become out of range.","commonSituations":"Using a cached vertex count/index after prior removeVertex calls shrank the matrix; off-by-one when mapping a label to an index; passing the vertex label instead of its numeric index; forgetting that indices shift after deletion.","solutions":["Guard the call: if (index >= 0 && index < graph.size()) graph.removeVertex(index);","Re-resolve indices from labels immediately before removal rather than caching them.","If removing multiple vertices by index, do so from highest to lowest to keep earlier indices stable.","Confirm you are passing a numeric index, not a vertex label object."],"exampleFix":"// before\ngraph.removeVertex(index); // index may be >= size() -> throws\n\n// after\nif (index >= 0 && index < graph.size()) {\n  graph.removeVertex(index);\n}","handlingStrategy":"validation","validationCode":"function canRemoveVertexIndex(graph, index) {\n  return Number.isInteger(index) && index >= 0 && index < graph.size();\n}\nif (canRemoveVertexIndex(graph, i)) graph.removeVertex(i);","typeGuard":"function isValidVertexIndex(graph, index) {\n  return typeof index === 'number' && Number.isInteger(index) && index >= 0 && index < graph.size();\n}","tryCatchPattern":"try {\n  graph.removeVertex(i);\n} catch (e) {\n  if (e instanceof RangeError) { /* out of range */ }\n  else throw e;\n}","preventionTips":["Re-resolve index from label right before removal.","Remove multiple vertices highest-index-first.","Pass a numeric index, not a vertex label."],"tags":["graph","adjacency-matrix","bounds-check","range-error","javascript"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}