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 removeVertex(index) on an adjacency-matrix graph when index >= size() (the number of vertices). Note the guard checks only the upper bound (index >= size) and omits the negative-index check, but the subsequent splice with a negative index would behave unexpectedly. It removes the vertex's row/column from the matrix.
Source
Thrown at en/codes/javascript/chapter_graph/graph_adjacency_matrix.js:52
const n = this.size();
// Add the value of the new vertex to the vertex list
this.vertices.push(val);
// Add a row to the adjacency matrix
const newRow = [];
for (let j = 0; j < n; j++) {
newRow.push(0);
}
this.adjMat.push(newRow);
// Add a column to the adjacency matrix
for (const row of this.adjMat) {
row.push(0);
}
}
/* Remove vertex */
removeVertex(index) {
if (index >= this.size()) {
throw new RangeError('Index Out Of Bounds Exception');
}
// Remove the vertex at index from the vertex list
this.vertices.splice(index, 1);
// Remove the row at index from the adjacency matrix
this.adjMat.splice(index, 1);
// Remove the column at index from the adjacency matrix
for (const row of this.adjMat) {
row.splice(index, 1);
}
}
/* Add edge */
// Parameters i, j correspond to the vertices element indices
addEdge(i, j) {
// Handle index out of bounds and equality
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
- Guard the call: if (index >= 0 && index < graph.size()) graph.removeVertex(index);
- Re-resolve indices from labels immediately before removal rather than caching them.
- If removing multiple vertices by index, do so from highest to lowest to keep earlier indices stable.
- Confirm you are passing a numeric index, not a vertex label object.
Example fix
// before
graph.removeVertex(index); // index may be >= size() -> throws
// after
if (index >= 0 && index < graph.size()) {
graph.removeVertex(index);
} Defensive patterns
Strategy: validation
Validate before calling
function canRemoveVertexIndex(graph, index) {
return Number.isInteger(index) && index >= 0 && index < graph.size();
}
if (canRemoveVertexIndex(graph, i)) graph.removeVertex(i); Type guard
function isValidVertexIndex(graph, index) {
return typeof index === 'number' && Number.isInteger(index) && index >= 0 && index < graph.size();
} Try / catch
try {
graph.removeVertex(i);
} catch (e) {
if (e instanceof RangeError) { /* out of range */ }
else throw e;
} Prevention
- Re-resolve index from label right before removal.
- Remove multiple vertices highest-index-first.
- Pass a numeric index, not a vertex label.
When it happens
Trigger: Calling removeVertex(index) with index >= graph.size(), or with a negative index (not explicitly guarded, may cause splice quirks rather than a clean throw). After removals, a previously valid index can become out of range.
Common situations: Using a cached vertex count/index after prior removeVertex calls shrank the matrix; off-by-one when mapping a label to an index; passing the vertex label instead of its numeric index; forgetting that indices shift after deletion.
Related errors
- Index Out Of Bounds Exception
- Index Out Of Bounds Exception
- Index Out Of Bounds Exception
- 顶点索引越界
- 边索引越界或相等
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/988f88ef511a9876.
Report an issue: GitHub.