krahets/hello-algo · error · RangeError

Index Out Of Bounds Exception

Error message

Index Out Of Bounds Exception

What it means

Thrown by GraphAdjacencyMatrix.removeVertex (JS) as a RangeError when the index argument is out of range. It splices both the vertex row and its matrix column, so an invalid index would desync vertices and adjMat. NOTE: the guard only checks index >= size(); a negative index passes the check and produces silent corruption rather than this error.

Source

Thrown at ru/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 bounds yourself including negatives: if (index >= 0 && index < g.size()) g.removeVertex(index).
  2. Re-fetch size() immediately before the call rather than caching it.
  3. Prefer looking up the vertex by value rather than by shifting index when possible.
  4. Report the missing negative-index guard upstream as a bug.

Example fix

// before
g.removeVertex(idx); // throws RangeError when idx >= size

// after
if (idx >= 0 && idx < g.size()) g.removeVertex(idx);
else throw new RangeError('bad vertex index: ' + idx);
Defensive patterns

Strategy: validation

Validate before calling

// NOTE: the library only checks index >= size; you must also reject negatives
if (index >= 0 && index < g.size()) g.removeVertex(index);
else throw new RangeError('Invalid vertex index: ' + index);

Type guard

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

Prevention

When it happens

Trigger: removeVertex(index) called with index >= this.size() (e.g., size 5 and index 5). A negative index does NOT trigger this error — it slips through and corrupts the matrix.

Common situations: Off-by-one after addVertex/removeVertex shifts indices; stale cached index into a matrix whose size changed; iterating vertices.length without accounting for a prior removal.

Related errors


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