Errors
Luma has two kinds of errors:
| Kind | When | Example |
|---|---|---|
| compile error | before the program runs: the program isn’t valid Luma | a misspelled name, mixing int and str |
| runtime error | while the program runs: an operation is impossible | division by zero, a list index outside the list |
Both print a message with the place in the file where the problem is:
file.luma:LINE:COLUMN: messageBoth make luma (or the compiled program) exit with code 1.
Compile errors
When a program has a compile error, nothing runs, not even the lines before the error. Luma reports the first error it finds.
Some examples:
hello.luma:1:1: unknown function "prnt"
hello.luma:3:9: operator + needs both sides to have the same type, got int and str
hello.luma:2:7: undefined variable cout
hello.luma:4:4: condition must be a bool, got int (compare explicitly, e.g. x > 0)
hello.luma:1:11: 256 does not fit in a byte (0..255)
hello.luma:2:6: '=' assigns a value; use '==' to compare
hello.luma:5:1: x is already declared at 1:1
hello.luma:1:7: a number cannot start with '.' (write 0.5)The messages try to say what to do, not only what is wrong.
Runtime errors
A runtime error stops the program at that point, unless it happens inside a try block, which can catch it. Everything printed before it stays printed:
print("before")
zero: int = 0
print(1 / zero)
print("after")Output:
before
example.luma:3:9: runtime error: division by zeroThe message goes to the error output (stderr), and the program exits with code 1. A compiled program prints the very same message, including the file name and position.
List of runtime errors
| Code | Error | Cause |
|---|---|---|
| 1 | runtime error: division by zero | / or % on int or byte with a zero right side |
| 2 | runtime error: index N out of range (length M) | reading, changing or removing a list element that doesn’t exist, including negative indexes |
| 3 | runtime error: key K not found | reading or removing a map key that isn’t in the map (check with has first) |
| 3 | runtime error: element E not found | removing a value that isn’t in the set |
| 4 | runtime error: cannot throw nil / cannot read .code of nil | using a nil error |
| 5 | runtime error: error code N is reserved ... | error(code, ...) with a code below 100 |
| 6 | runtime error: invalid input "TEXT": expected an int | prompt read a line that isn’t a value of the type wanted |
| 7 | runtime error: end of input | prompt with nothing left to read |
| 8 | runtime error: file not found: PATH | reading, deleting or getting the size of a file that isn’t there |
| 9 | runtime error: cannot write PATH: REASON | a file operation failed: permission denied, it is a folder, the folder does not exist, … |
| 10 | runtime error: too many nested calls (more than 10000) | a function calls itself (or others) too deep, usually a recursion that never stops |
| 11 | runtime error: cannot convert "abc" to an int | convert of a value that doesn’t fit the type; also JSON that isn’t valid or not of the kind used |
| 18 | runtime error: files and folders aren't available in the playground (or the network, or running other programs) | a program run with luma run -sandbox, as the playground does, tried to reach the system |
| – | runtime error: out of memory | the operating system has no more memory to give (compiled programs only; can’t be caught) |
All of these except out of memory can be caught with try; the code is then in err.code. Errors your program raises with throw are reported as error CODE: message when nothing catches them. See Error handling.
Float division by zero is not an error: it gives inf or nan.