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 zero

The error type

An error variable holds either nil (no error) or an error with two fields:

FieldTypeMeaning
err.codeinta number that says what kind of error it is
err.messagestra description for people
err: error = nil
print(err)
print(err == nil)

Output:

nil
true
  • nil can only be used for errors; x: int = nil is a compile error.
  • Errors can only be compared with nil: err == nil and err != nil. To tell errors apart, compare their codes.
  • print(err) shows error CODE: message, or nil.
  • Reading .code or .message of nil is 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 err gets the error.
  • If the block finishes normally, err is set to nil.

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
nil

Rules:

  • The variable must already be declared as an error: err: error = nil.
  • Write it without parentheses: try err { }, just as if is 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 full

Codes 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 here

throw 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.txt

Luma’s own errors are reported as runtime error: .... See Errors. In both cases, the program exits with code 1.

Luma’s error codes

CodeErrorExample message
1division by zero (int and byte only), or a shift by a negative numberdivision by zero, cannot shift by -1
2list index out of rangeindex 5 out of range (length 2)
3map key or set element not foundkey "Carol" not found, element 5 not found
4a nil error usedcannot throw nil, cannot read .code of nil
5a reserved code used in error()error code 5 is reserved for Luma's own errors (use 100 or more)
6invalid input for promptinvalid input "forty": expected an int
7nothing left to read for promptend of input
8a file that isn’t therefile not found: notes.txt
9a file operation failedcannot write data/notes.txt: the folder does not exist
10more than 10,000 nested function callstoo many nested calls (more than 10000)
11a 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 fitcannot convert "abc" to an int
12date(...), time(...) or datetime(...) with numbers that make no date or timeinvalid date: day 30 is not in February 2024 (1..29)
13reading past the end of a buffer, or a number that doesn’t fit its bytescannot read 4 bytes at position 0: the buffer has 2
14a network connection or web request failed, closed too early or timed outcannot connect to db:5432: connection refused
15a database said no, or SQL and its values don’t fitno value for :min: give it with the SQL, e.g. {"min": ...}
16a command failed (an exit code other than 0) or couldn’t be runcommand failed (exit 2): ls: cannot access '/nope': No such file or directory
17an assert whose condition is falseassert 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
nil

Errors 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]