Skip to content

Error

Built-in class · Every error extends it

An Error is a value that describes something that went wrong. raise stops the program’s usual path with one, and try and catch can handle it. Every error in Emerald, built in or written by a program, is an Error or extends one.

class InvalidScore extends Error {
}
func check(score: Int) {
if score < 0 {
raise InvalidScore("Score cannot be negative")
}
}
try {
check(-5)
}
catch error: InvalidScore {
print(error.message) # prints Score cannot be negative
}

A program makes its own kinds of error by extending Error, as InvalidScore does above. An error class with no fields of its own is built with just its message; one that adds fields writes a constructor that starts with super(message).

Only an Error can be raised: raise 1 is an error before the program runs. When nothing catches an error, the program stops and shows the message, where it happened, and the functions that led there.

message: String

What went wrong, in words.

Emerald’s own errors are RuntimeError, for a problem found while the program runs, and AssertionError, for a failed assert. Three kinds of RuntimeError come up in everyday programs: EncodingError, for bytes or text that can’t be converted, InputError, for input that ends or isn’t text, and RecursionError, for a function that calls itself too many times. The standard library adds its own, and CancelledError extends Error directly, so catching RuntimeError doesn’t catch a cancelled task.