Exceptions
Throwing
Custom exception types
exception is a variant type, so you can add new case types.
exception has a single method show.
(See Interfaces and variants.)
Assert and forbid
assert throws an error if its argument is false.
forbid throws an error if its argument is true.
An optional else clause specifies the exception to throw.
assert and forbid work with option conditions.
The option from an assert will be available as a non-option after it.
The option from a forbid will be available as a non-option in its else clause.
is is also a kind of assertion. Prefer this over assert x == y.
Finally
A finally expression has right and below subexpressions.
If first runs the below expression.
It then runs the right expression, whether or not the below expression threw an exception.
Below, the right expression is log "right"
and the below expression is the sequence
log "below" and log 1 / 0 .
The value of a finally expression is the value of the below expression.
The right expression must be void.
The right expression can be an indented block.
Below, the right expression is on lines 3 and 4
and the below expression is just log 1 / 0 .
Catching exceptions
A try expression first evaluates the try block.
If that threw the caught exception type, it evaluates the catch block instead.
The catch has no effect if some other kind of exception is thrown.
A local name between catch and as will have the exception value.
There can be multiple catch blocks for different exception types.
If there is no as clause, it catches every exception.
Common exceptions
Throw not-implemented if something is intentionally not implemented.
Throw todo if something hasn't been implemented, but should be.
Throw unreachable
if it should not be possible to get to the throw expression.
Testing exceptions
Use this pattern to test functions that should throw exceptions: