Exceptions
Jade handles exceptions with three keywords. raise throws any value as an exception. try wraps a block that might raise. catch handles the raised value, either by type or for anything at all.
Overview
An exception is any Jade value thrown with the raise statement. That value can be a string, a struct instance, an integer, or anything else. The usual pattern is to define a dedicated struct type to carry the details, then raise an instance of it. Doing so lets catch arms match by type name and read the fields of the caught value.
A try and catch block wraps the statements that might raise. If the try body finishes without raising, the program continues after the last catch arm and no arm runs. If something raises, the try body stops at once, and the runtime checks the catch arms in order and runs the first one that matches.
Jade wraps its own runtime errors in a RuntimeError struct with a single message field. That covers division by zero, type errors, undefined variable references, index out of bounds, and every other built-in error. A catch-all arm, written catch e { … }, catches these the same way it catches the exceptions you raise yourself.
If no catch arm matches, the exception travels outward to the nearest enclosing try and catch. If it reaches the top level uncaught, the program prints unhandled exception: <value> and exits with status 1.
Syntax
raise
raise <expr>
<expr> is any expression. Its value becomes the raised exception, and the enclosing block stops running right there.
try / catch
try {
<body statements>
} catch <TypeName> <binding> {
<arm statements>
} catch <binding> {
<arm statements>
}
<body statements>is any sequence of statements that might raise.<TypeName>is an optional struct type name. With a name, the arm matches only instances of that struct type. Without one, the arm is a catch-all and matches anything raised.<binding>is a name bound to the caught value inside the arm body.
You can write any number of typed catch arms before the optional catch-all. Jade tests them in the order you wrote them, and the first match wins. Put the catch-all last, because it matches everything.