krahets/hello-algo · error · RangeError

Index Out Of Bounds Exception

Error message

Index Out Of Bounds Exception

What it means

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.

Source

Thrown at ja/codes/javascript/chapter_graph/graph_adjacency_matrix.js:52

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

    /* 頂点を削除 */
    removeVertex(index) {
        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, j) {
        // インデックスの範囲外と等値の処理
        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. Validate 0 <= index < graph.size() before calling removeVertex.
  2. Re-derive the index from vertices.indexOf(...) immediately before each removal rather than caching it.
  3. If you need to remove by value, look up vertices.indexOf(value) first and only remove when found.
  4. Refresh any cached indices after every structural change to the graph.

Example fix

// before
graph.removeVertex(idx);  // idx may be stale/out of range

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

Strategy: validation

Validate before calling

function safeRemoveVertex(graph, index) {
  if (index < 0 || index >= graph.size()) return false;
  graph.removeVertex(index);
  return true;
}

Type guard

const isValidIndex = (graph, i) => Number.isInteger(i) && i >= 0 && i < graph.size();

Try / catch

try {
  graph.removeVertex(index);
} catch (e) {
  if (e instanceof RangeError) {
    // index out of bounds; recompute and retry once
  } else throw e;
}

Prevention

When it happens

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

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

Related errors


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