The Zeno Book Zeno 0.1 zeno-lang.org

F Kinds of Error

Zeno meets a mistake at one of three times: before the program runs, while it runs, or, in a compiled program, as a fault that stops it. This appendix lists what can be met at each, with the message.

F.1 Before the program runs

The lexer, the parser and the checker find mistakes in the text. Each is reported with the file, the line and the column, the line itself, and a mark under the place, and nothing is run:

let count: int = "three"
error: `count` should be int, but this is str
 --> main.zeno:1:18
  |
1 | let count: int = "three"
  |                  ^

1 error found. Nothing was run.

The lexer and the checker report every mistake they are sure of; the parser stops at the first (Inside Zeno). zeno check finds the same mistakes as zeno run and stops there.

F.2 The kinds raised while it runs

An error raised while the program runs has a kind, which is what except names (When Things Go Wrong). The kinds Zeno raises itself are these:

Kind Raised when
ValueError int or float is given text that is not a number; the program raises it for a value it refuses
IndexError a position is past the end of a list or text, or below zero; pop on an empty list
KeyError a dict is asked for a key it does not have
ZeroDivisionError /, // or % by zero
OverflowError a result does not fit in its type: an int past 263 − 1, or a sized number past its range
FileError a file cannot be read or written: it is not there, or it may not be opened

A program can raise any of these itself, and an object of any class of its own. except Error catches every kind, and inside it e.kind is the kind’s name and e.message the text.

F.3 When nothing catches it

An error that no except catches ends the program. It prints uncaught, the kind and the message, and where it was raised, and the program’s exit status is 1:

let ages = ["Ada": 36]
print(ages["Grace"])
error: uncaught KeyError: this dict has no key "Grace"
 --> main.zeno:2:7
  |
2 | print(ages["Grace"])
  |       ^
let readings = [3, 4]
print(readings[2])
error: uncaught IndexError: index 2 is out of range for a list of length 2
 --> main.zeno:2:7
  |
2 | print(readings[2])
  |       ^
var n = 9223372036854775807
n += 1
error: uncaught OverflowError: the result is too big for an int
 --> main.zeno:2:1
  |
2 | n += 1
  | ^

A compiled program prints these same lines, byte for byte, in every kind of build.

F.4 Faults in a firmware build

In a build with --firmware or --no-collector, only raise, int, float and FileError travel as values that except can catch. Every other kind above is a fault: the program stops with the same message, and a try that hopes to catch one is refused before anything is built (Firmware).

F.5 Messages from memory

These come only from builds, or from unsafe code, and are always faults:

Message Means
the memory pool is full a build with no collector needed more than ZENO_POOL bytes at once (Building with No Collector)
a statement built more text than there is room for one statement built more text than ZENO_TEXT_ROOM holds
a block of memory was given back twice the runtime found a double free
out of memory malloc failed, in a build with a collector
the simulated board remembers … more registers were written on the simulated board than it keeps

Under zeno run, unsafe code is watched more closely than any compiled program can be, and each message says what a compiled program would have done instead (Unsafe Code):

let a = allocate(u8, 4)
unsafe {
    a[9] = 1
}
error: this writes over item 9 of a block that has 4 (0 to 3). `zeno run` checks every use of a pointer. A compiled program does not: it writes over whatever is beside the block
 --> main.zeno:3:5
  |
3 |     a[9] = 1
  |     ^
let a = allocate(u8, 4)
unsafe {
    free(a + 1)
}
error: `free` needs the pointer that `allocate` gave, and this one points 1 item further on. `zeno run` checks every `free`. A compiled program does not
 --> main.zeno:3:5
  |
3 |     free(a + 1)
  |     ^