Runtime errors¶
When a program fails while running, it prints the error to stderr the way the checker prints
one, with the file, line, and column, the message, and the source line with carets under the
expression that failed, then the calls that led there, and exits with status 1. Output printed before the error stays
printed. A compiled executable prints exactly what the interpreter does, since stone build
writes each place an error can happen into the executable:
$ stone run average.stn
7
average.stn:5:9: error: division by zero
|
5 | ret total / values.len()
| ^^^^^^^^^^^^^^^^^^^^
average.stn:8:7: called from here
|
8 | print(average([]))
| ^^^^^^^^^^^
$ stone build average.stn -o average && ./average
7
average.stn:5:9: error: division by zero
|
5 | ret total / values.len()
| ^^^^^^^^^^^^^^^^^^^^
average.stn:8:7: called from here
|
8 | print(average([]))
| ^^^^^^^^^^^
Both backends stop at the same point with the same message, since operands are evaluated left
to right in both. The error points at the innermost expression that failed: the division in
1 + a / b, the subscript in xs[i], the call in xs.pop(i), x.unwrap(), print(x), or
math.sqrt(x), and
for recursion is too deep, the call that went one level too deep. A change such as
grid[i][j] = v or ps[i].x = v points at the subscript whose index is out of range. An executable names the
file by the path it was built from.
Calls that led to an error¶
After the error come the calls that led to it, innermost first, each as
file:line:col: called from here and the call’s line with carets under it. Every call of a
function written in stone counts, in any file of the program: functions, methods, associated
functions, function values, lambdas, and closures, operators and conversions a struct
implements, and the calls stone makes for you, such as print or a string’s {value} calling a
struct’s display, == calling its eq, and < or sort calling its cmp. Calls made inside
the prelude and the builtin modules written in stone (net, gui, json) are left out, and so
is the call of a test by stone test.
A run of calls from one place, as in recursion, is written once with a count:
countdown.stn:4:13: error: division by zero
|
4 | ret 10 / n
| ^^^^^^
countdown.stn:5:9: called from here (3 times)
|
5 | ret countdown(n - 1)
| ^^^^^^^^^^^^^^^^
countdown.stn:8:7: called from here
|
8 | print(countdown(3))
| ^^^^^^^^^^^^
At most 20 calls are written, a run counting as one, then ... and 11 more calls says how many
calls are left out. A compiled executable writes the same calls as stone run, even where
stone build compiled a small function into its caller, and finds them by walking its own
stack, allocating nothing.
message |
cause |
|---|---|
|
|
|
the smallest int divided by |
|
|
|
an index at or past the length, or a negative index before the start, including |
|
|
|
|
|
|
|
a |
|
a string index at or past the length, or a negative index before the start. It gives the index as written and the string’s length in bytes |
|
|
|
a slice such as |
|
arithmetic on two lists, such as |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
more than 1,000 calls active at once |
|
|
|
|
|
|
|
|
|
|
|
a function read a global that had not been assigned yet when it ran |
Arithmetic on lists computes the whole expression for the first element, then the second, and
so on, so an error comes from the first element where any of its operations fails. In
a / b + c / d, a zero in d at index 1 stops the program before a zero in b at index 2.
Such an error points at the whole list expression, a / b + c / d, while a number computed
once inside it, such as (n / m) in xs * (n / m), fails at its own expression.
Integer +, -, *, and ** wrap around on overflow instead of failing. Float arithmetic
follows IEEE 754 apart from division by zero and the math domain errors above, so it produces inf, -inf, and nan instead of
errors.
A float ** multiplies by repeated squaring and takes the reciprocal for a negative exponent, so
0.0 ** -1 is inf.
A ret at the top level is not an error: it ends the program with status 0.