M Mozhi v3.7.0 code

Error reference › Runtime diagnostics

RUNTIME003

index out of range

error
verifiedverified on v3.7.0 categoryruntime terminalstable code

What it means

Arrays and strings are zero-based. Indexing beyond the end (or with a negative number) has no defined element, so it is an error — unlike map indexing, which returns null for missing keys by design.

Trigger condition

An array or string index is outside `0..length-1`, or a negative index is used.

Minimal failing example

let a = [1, 2, 3]
echo(a[5])

Verified fix

let a = [1, 2, 3]
if 5 < len(a) { echo(a[5]) }

Bounds-check with `len(a)` before indexing; iterate with `for x in a` to avoid manual indexes.

What the interpreter prints

error[RUNTIME003]: index out of range
  --> app.mz:1:16
 1 | let a = [1, 2, 3]
   |                 ^
   |
reason: (the exact problem, with positions)
help:   (a suggested fix appears here)
docs:   https://crossberry-in.github.io/mozhi-doc/errors/RUNTIME003
Run mozhi --json run app.mz to get the same information as machine-readable JSON (schema mozhi.diagnostics.v1), or --verbose to also show grouped related diagnostics. See the CLI reference.

Suggested fix

Validate the index, clamp it, or iterate the collection instead of indexing.

Related syntax

Arrays, strings, for-in loops

Related diagnostics

RUNTIME001

Reference links

Limitations

Mozhi has no negative indexing (`a[-1]` is an error, not 'last element').