krahets/hello-algo · error · RangeError

Index Out Of Bounds Exception

Error message

Index Out Of Bounds Exception

What it means

Thrown by GraphAdjMatrix.removeVertex(index) when index >= size(). The method splices the vertex out of both the vertices array and the adjacency matrix. Note: the guard checks only index >= size() — it does NOT check index < 0, so a negative index bypasses the guard and splice treats it as an offset-from-end, silently corrupting the matrix.

Source

Thrown at zh-hant/codes/javascript/chapter_graph/graph_adjacency_matrix.js:52

        const n = this.size();
        // 向頂點串列中新增新頂點的值
        this.vertices.push(val);
        // 在鄰接矩陣中新增一行
        const newRow = [];
        for (let j = 0; j < n; j++) {
            newRow.push(0);
        }
        this.adjMat.push(newRow);
        // 在鄰接矩陣中新增一列
        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. Never pass negative indices — the built-in guard does not reject them.
  3. If you hold a vertex value, resolve its positional index via vertices.indexOf(value) first.

Example fix

// before
graph.removeVertex(index); // throws if index >= size()

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

Strategy: validation

Validate before calling

// Full bounds check including negative index (the built-in guard misses negatives)
if (index >= 0 && index < graph.size()) {
    graph.removeVertex(index);
}

Type guard

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

Try / catch

try {
    graph.removeVertex(index);
} catch (e) {
    if (e instanceof RangeError) {
        // index out of bounds — handle
    } else throw e;
}

Prevention

When it happens

Trigger: Passing an index >= the current vertex count, or passing a negative index (which slips past the incomplete guard and corrupts data).

Common situations: Off-by-one after batch removals; confusing a vertex's value/label with its positional index; stale size assumption after vertices were removed; using -1 as a sentinel that bypasses the check.

Related errors


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