Error handling
Some things can go wrong while a program runs: a division by zero, a list index that doesn’t exist, a missing map key, or a problem your own program detects. Without error handling, any of these stops the program. With try, your program can catch the error and carry on.
err: error = nil
try err {
a: int = 10
b: int = 0
print("before")
print(a / b)
print("never")
}
if err != nil {
print(err.code)
print(err.message)
}Output:
before
1
division by zeroThe error type
An error variable holds either nil (no error) or an error with two fields:
| Field | Type | Meaning |
|---|---|---|
err.code | int | a number that says what kind of error it is |
err.message | str | a description for people |
err: error = nil
print(err)
print(err == nil)Output:
nil
truenilcan only be used for errors;x: int = nilis a compile error.- Errors can only be compared with
nil:err == nilanderr != nil. To tell errors apart, compare their codes. print(err)showserror CODE: message, ornil.- Reading
.codeor.messageofnilis itself an error (code 4).
try
try err {
...
}try runs the block:
- If an error is raised anywhere inside it, the rest of the block is skipped, and
errgets the error. - If the block finishes normally,
erris set tonil.
So after a try, err always tells you what happened in that block, even if it held an older error before.
err: error = nil
nums: [int] = [1, 2]
try err {
print(nums[5])
}
print(err)
try err {
print(nums[0])
}
print(err)Output:
error 2: index 5 out of range (length 2)
1
nilRules:
- The variable must already be declared as an
error:err: error = nil. - Write it without parentheses:
try err { }, just asifis written without them. - Statements that ran before the error are not undone. If the block changed a variable or added to a list before the error, those changes stay.
throw
throw raises an error. Make one with error(code, message):
err: error = nil
try err {
throw error(100, "Disk is full")
}
print(err.code)
print(err.message)Output:
100
Disk is fullCodes below 100 are reserved for Luma’s own errors (listed below). Your own codes start at 100. Using a smaller code is a compile error, or error 5 if the code is only known when the program runs.
Passing an error on
throw err raises a caught error again. This is useful when a block can handle some errors but not others:
err: error = nil
outer: error = nil
try outer {
try err {
throw error(101, "cannot handle this here")
}
if err != nil && err.code != 100 {
throw err
}
print("never")
}
print(outer)Output:
error 101: cannot handle this herethrow nil is an error (code 4).
Uncaught errors
An error that no try catches stops the program, printing where it was raised:
print("start")
throw error(404, "Not found: config.txt")Output:
start
example.luma:2:1: error 404: Not found: config.txtLuma’s own errors are reported as runtime error: .... See Errors. In both cases, the program exits with code 1.
Luma’s error codes
| Code | Error | Example message |
|---|---|---|
| 1 | division by zero (int and byte only), or a shift by a negative number | division by zero, cannot shift by -1 |
| 2 | list index out of range | index 5 out of range (length 2) |
| 3 | map key or set element not found | key "Carol" not found, element 5 not found |
| 4 | a nil error used | cannot throw nil, cannot read .code of nil |
| 5 | a reserved code used in error() | error code 5 is reserved for Luma's own errors (use 100 or more) |
| 6 | invalid input for prompt | invalid input "forty": expected an int |
| 7 | nothing left to read for prompt | end of input |
| 8 | a file that isn’t there | file not found: notes.txt |
| 9 | a file operation failed | cannot write data/notes.txt: the folder does not exist |
| 10 | more than 10,000 nested function calls | too many nested calls (more than 10000) |
| 11 | a value convert can’t fit into the type, or text that isn’t hex or Base64 (crypto), or a random range that is empty, or an environment variable or .env line that doesn’t fit | cannot convert "abc" to an int |
| 12 | date(...), time(...) or datetime(...) with numbers that make no date or time | invalid date: day 30 is not in February 2024 (1..29) |
| 13 | reading past the end of a buffer, or a number that doesn’t fit its bytes | cannot read 4 bytes at position 0: the buffer has 2 |
| 14 | a network connection or web request failed, closed too early or timed out | cannot connect to db:5432: connection refused |
| 15 | a database said no, or SQL and its values don’t fit | no value for :min: give it with the SQL, e.g. {"min": ...} |
| 16 | a command failed (an exit code other than 0) or couldn’t be run | command failed (exit 2): ls: cannot access '/nope': No such file or directory |
| 17 | an assert whose condition is false | assert failed: n > 0 |
Running out of memory can’t be caught; it always stops the program.
Nesting
try blocks can be nested. An error goes to the innermost try around it:
err: error = nil
outer: error = nil
nums: [int] = [1]
try outer {
try err {
print(nums[3])
}
print("the inner try caught it")
}
print(err.code)
print(outer)Output:
the inner try caught it
2
nilErrors are values
An error can be stored like any other value: in another variable, in a list, or in a map.
err: error = nil
try err {
throw error(250, "first problem")
}
log: [error] = [err, nil]
print(log)Output:
[error 250: first problem, nil]