Language guide · 9 of 12

Error Handling

Something that goes wrong is an error value. throw raises one, and catch turns one back into a value you can look at. Every error says what went wrong, what sort of failure it is, and where it happened.

Throwing Errors

fn divide(a, b) {
    if b == 0 {
        throw error("division by zero")
    }
    return a / b
}
Open in playground

error(message, fields) also takes an object of fields. kind names the sort of failure, cause is the error this one was raised because of, and any other name is data the catcher can read:

throw error("cannot load the config", {kind: "config", path: path, cause: failure})
Open in playground

Throwing something that is not an error makes one: a string is the message, an object gives its fields as above (with message as the message), and any other value is kept as the field value. So throw {kind: "http", code: 404} arrives with e.kind == "http" and e.code == 404.

Catching Errors

catch(expr) runs an expression and gives back its value, or the error it raised. catch { ... } does the same for a block of statements, and is nil when the block finishes without an error. A return, break or continue inside the block works as it would outside it.

result := catch(divide(10, 0))
if result is error {
    io.println("Failed:", result.message, "at", result.file, result.line)
}

var config := {}
failure := catch {
    text := fs.readText("app.json")
    config = json.decode(text)
}
Open in playground

What an Error Holds

field is
message what went wrong
kind the sort of failure, such as "notFound"; "error" unless one was given
cause the error, or other value, this one was raised because of; nil when there is none
file, line, column where it was thrown
trace the functions that were running, from the innermost outwards, each with its position
any other the fields it was given, as e.path above; e.path? tests for one

Errors are values: they can be stored, passed around and thrown again. Throwing a caught error keeps its original position and trace.

Kinds

The kind is how code tells failures apart, without depending on the wording of a message. Evie and its standard library use these:

kind for
"type" a value of the wrong type, a call with the wrong number of arguments, a constant assigned
"value" the right type but an unusable value, as in number("two")
"range" an index outside an array, string or buffer
"member" a field, key, method or package member that does not exist
"notFound", "exists", "permission", "io" the fs package: a missing file, one that already exists, access not allowed, any other failure; the error's path names the file
"timeout" await.timeout that ran out of time
"cancelled" a task that was cancelled
"syntax" text that does not parse, for json.decode and time.parse
"error" everything else, and errors made without a kind

Your own kinds work the same way:

fn load(path) {
    text := catch(fs.readText(path))
    if text is error {
        throw error("cannot load the config", {kind: "config", path: path, cause: text})
    }
    return json.decode(text)
}

e := catch(load("app.json"))
if e is error && e.cause.kind == "notFound" {
    io.println("no config at", e.path, "- using the defaults")
}
Open in playground

Uncaught Errors

Uncaught errors reach the host with the same information. The evie command prints them with their kind, unless it is "error", the offending line, the span underlined and labelled, related places, a hint when one applies, and the trace:

error[type]: cannot apply '+' to a number and a string
 --> example.ev:6:9
  |
6 |         sum += v
  |         ^^^^^^^^ number + string
  |
  = help: convert the other side with string(...) to concatenate, or with number(...) to add
  = trace: average at example.ev:6:9
           report at example.ev:11:22
           main at example.ev:14:5
Open in playground

A Go host gets the same rendering from any error that implements Diagnostic() *diag.Report: report.Render(diag.Colors) for a terminal, diag.Palette{} for plain text, and err.Error() for the one-line form with the location in parentheses. The host also reads an error's kind, fields and cause, and gives errors its own (see Go Host Embedding).

Edit this page on GitHub