krahets/hello-algo · error · RangeError

Index Out Of Bounds Exception

Error message

Index Out Of Bounds Exception

What it means

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.

Source

Thrown at ja/codes/typescript/chapter_graph/graph_adjacency_matrix.ts:52

        const n: number = this.size();
        // 頂点リストに新しい頂点の値を追加
        this.vertices.push(val);
        // 隣接行列に 1 行追加
        const newRow: number[] = [];
        for (let j: number = 0; j < n; j++) {
            newRow.push(0);
        }
        this.adjMat.push(newRow);
        // 隣接行列に 1 列追加
        for (const row of this.adjMat) {
            row.push(0);
        }
    }

    /* 頂点を削除 */
    removeVertex(index: number): void {
        if (index >= this.size()) {
            throw new RangeError('Index Out Of Bounds Exception');
        }
        // 頂点リストから index の頂点を削除する
        this.vertices.splice(index, 1);

        // 隣接行列で index 行を削除する
        this.adjMat.splice(index, 1);
        // 隣接行列で index 列を削除する
        for (const row of this.adjMat) {
            row.splice(index, 1);
        }
    }

    /* 辺を追加 */
    // 引数 i, j は vertices の要素インデックスに対応する
    addEdge(i: number, j: number): void {
        // インデックスの範囲外と等値の処理
        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {
            throw new RangeError('Index Out Of Bounds Exception');

View on GitHub (pinned to 69932aed18)

Solutions

  1. Recompute or clamp the index against `graph.size()` before calling removeVertex.
  2. Add a lower-bound check `index < 0` to your own guard, since the library guard does not cover negatives.
  3. When iterating for batch deletion, delete from highest index to lowest so earlier indices stay valid.
  4. Validate user-supplied or deserialized indices before touching the graph.

Example fix

// before
matrix.removeVertex(idx);

// after
if (idx >= 0 && idx < matrix.size()) {
  matrix.removeVertex(idx);
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate upper AND lower bounds; the library guard omits index < 0.
if (index >= 0 && index < matrix.size()) {
  matrix.removeVertex(index);
}

Type guard

function isValidVertexIndex(matrix, index) {
  return Number.isInteger(index) && index >= 0 && index < matrix.size();
}

Try / catch

try {
  matrix.removeVertex(index);
} catch (e) {
  if (e instanceof RangeError && /Index Out Of Bounds/.test(e.message)) {
    // handle bad index
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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`.

Related errors


AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13). Data as JSON: /api/errors/5d2514fa7253d136. Report an issue: GitHub.