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 the adjacency-matrix graph (RangeError) when index >= this.size(), i.e. index is at or past the vertex count. It splices the vertex out of the vertices array and removes the matching row and column from adjMat. Note the guard only checks the upper bound — it does NOT check index < 0, so a negative index passes the guard and splice() treats it as offset-from-end, which is a latent bug.

Source

Thrown at 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 both bounds yourself: if (i >= 0 && i < g.size()) g.removeVertex(i);.
  2. Iterate backward when removing multiple vertices by index so earlier indices stay valid.
  3. Always pass the numeric index, not the vertex value.
  4. Recompute size() after each removal; do not cache it.

Example fix

// before
g.removeVertex(i); // throws if i >= size
// after
if (i >= 0 && i < g.size()) g.removeVertex(i);
Defensive patterns

Strategy: validation

Validate before calling

function safeRemoveVertex(g, i) {
  if (i >= 0 && i < g.size()) g.removeVertex(i);
}

Type guard

function isVertexIndex(g, i): i is number {
  return typeof i === 'number' && Number.isInteger(i) && i >= 0 && i < g.size();
}

Try / catch

try {
  g.removeVertex(i);
} catch (e) {
  if (e instanceof RangeError && /Index Out Of Bounds/.test(e.message)) { /* skip */ } else throw e;
}

Prevention

When it happens

Trigger: Calling removeVertex with an index equal to size; reusing an index after a prior removal shifted vertices down; passing the vertex value instead of its index; negative index (does NOT throw here but corrupts the matrix).

Common situations: Off-by-one in deletion loops; iterating a vertices array while removing (indices shift); confusing vertex index with vertex value; stale size captured before shrinking.

Related errors


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