{"record":{"id":"123bffe24c231f91","repo":"krahets/hello-algo","slug":"index-out-of-bounds-exception-123bff","errorCode":null,"errorMessage":"Index Out Of Bounds Exception","messagePattern":"Index Out Of Bounds Exception","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"ja/codes/javascript/chapter_graph/graph_adjacency_matrix.js","lineNumber":52,"sourceCode":"        const n = this.size();\n        // 頂点リストに新しい頂点の値を追加\n        this.vertices.push(val);\n        // 隣接行列に 1 行追加\n        const newRow = [];\n        for (let j = 0; j < n; j++) {\n            newRow.push(0);\n        }\n        this.adjMat.push(newRow);\n        // 隣接行列に 1 列追加\n        for (const row of this.adjMat) {\n            row.push(0);\n        }\n    }\n\n    /* 頂点を削除 */\n    removeVertex(index) {\n        if (index >= this.size()) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // 頂点リストから index の頂点を削除する\n        this.vertices.splice(index, 1);\n\n        // 隣接行列で index 行を削除する\n        this.adjMat.splice(index, 1);\n        // 隣接行列で index 列を削除する\n        for (const row of this.adjMat) {\n            row.splice(index, 1);\n        }\n    }\n\n    /* 辺を追加 */\n    // 引数 i, j は vertices の要素インデックスに対応する\n    addEdge(i, j) {\n        // インデックスの範囲外と等値の処理\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/ja/codes/javascript/chapter_graph/graph_adjacency_matrix.js#L34-L70","documentation":"Thrown as a RangeError by the adjacency-matrix graph's removeVertex when the supplied index is outside the valid vertex range. The guard checks index >= size() (the current vertex count) before splicing the vertices array and the matrix rows/columns. A RangeError signals a numeric bound violation rather than a logical argument error.","triggerScenarios":"Calling removeVertex(index) with index >= graph.size(); passing a stale index after vertices were already removed and the matrix shrank; passing a negative index is NOT caught here (note the guard omits index < 0).","commonSituations":"Caching an index earlier and reusing it after removals shift the layout; off-by-one when iterating size(); passing a vertex label/value instead of its positional index.","solutions":["Validate 0 <= index < graph.size() before calling removeVertex.","Re-derive the index from vertices.indexOf(...) immediately before each removal rather than caching it.","If you need to remove by value, look up vertices.indexOf(value) first and only remove when found.","Refresh any cached indices after every structural change to the graph."],"exampleFix":"// before\ngraph.removeVertex(idx);  // idx may be stale/out of range\n\n// after\nif (idx >= 0 && idx < graph.size()) {\n  graph.removeVertex(idx);\n}","handlingStrategy":"validation","validationCode":"function safeRemoveVertex(graph, index) {\n  if (index < 0 || index >= graph.size()) return false;\n  graph.removeVertex(index);\n  return true;\n}","typeGuard":"const isValidIndex = (graph, i) => Number.isInteger(i) && i >= 0 && i < graph.size();","tryCatchPattern":"try {\n  graph.removeVertex(index);\n} catch (e) {\n  if (e instanceof RangeError) {\n    // index out of bounds; recompute and retry once\n  } else throw e;\n}","preventionTips":["Recompute indices via vertices.indexOf(value) immediately before removal.","Do not cache matrix indices across structural changes.","Note: the guard does not catch negative indices — validate index >= 0 yourself."],"tags":["graph","adjacency-matrix","javascript","index-out-of-bounds","range-error"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}