{"record":{"id":"5d2514fa7253d136","repo":"krahets/hello-algo","slug":"index-out-of-bounds-exception-5d2514","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/typescript/chapter_graph/graph_adjacency_matrix.ts","lineNumber":52,"sourceCode":"        const n: number = this.size();\n        // 頂点リストに新しい頂点の値を追加\n        this.vertices.push(val);\n        // 隣接行列に 1 行追加\n        const newRow: number[] = [];\n        for (let j: number = 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: number): void {\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: number, j: number): void {\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/typescript/chapter_graph/graph_adjacency_matrix.ts#L34-L70","documentation":"Thrown by removeVertex(index) on a graph adjacency matrix when `index >= this.size()`. It is a RangeError signalling the index is past the end of the vertices array. Note the guard checks only the upper bound — a negative index passes this check and will later produce a native splice no-op rather than this error.","triggerScenarios":"Calling removeVertex with an index greater than or equal to the number of vertices; calling removeVertex after the array has shrunk from prior deletions without recomputing the index; computing an index from an external position that exceeds the matrix dimension.","commonSituations":"Off-by-one when iterating `0..size` instead of `0..size-1`; stale index cached before a deletion; reading an index from user input or a deserialized matrix without clamping; negative index (e.g. -1) silently accepted because the guard omits `index < 0`.","solutions":["Recompute or clamp the index against `graph.size()` before calling removeVertex.","Add a lower-bound check `index < 0` to your own guard, since the library guard does not cover negatives.","When iterating for batch deletion, delete from highest index to lowest so earlier indices stay valid.","Validate user-supplied or deserialized indices before touching the graph."],"exampleFix":"// before\nmatrix.removeVertex(idx);\n\n// after\nif (idx >= 0 && idx < matrix.size()) {\n  matrix.removeVertex(idx);\n}","handlingStrategy":"validation","validationCode":"// Validate upper AND lower bounds; the library guard omits index < 0.\nif (index >= 0 && index < matrix.size()) {\n  matrix.removeVertex(index);\n}","typeGuard":"function isValidVertexIndex(matrix, index) {\n  return Number.isInteger(index) && index >= 0 && index < matrix.size();\n}","tryCatchPattern":"try {\n  matrix.removeVertex(index);\n} catch (e) {\n  if (e instanceof RangeError && /Index Out Of Bounds/.test(e.message)) {\n    // handle bad index\n  } else throw e;\n}","preventionTips":["Always check `index < 0` yourself — the library only checks the upper bound.","Recompute indices after any removeVertex since positions shift.","When batch-deleting, process indices in descending order.","Validate deserialized or user-supplied indices before use."],"tags":["graph","index-out-of-bounds","range-check","typescript"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}