Exception handling

Procedure: catch key thunk handler

Invoke thunk in the dynamic context of handler for exceptions matching key. If thunk throws to the symbol key, then handler is invoked this way:

(handler key args ...)

key may be a symbol. The thunk takes no arguments. If thunk returns normally, that is the return value of catch.

Handler is invoked outside the scope of its own catch. If handler again throws to the same key, a new handler from further up the call chain is invoked.

If the key is #t, then a throw to any symbol will match this call to catch.

Procedure: throw key &rest args ...

Invoke the catch form matching key, passing args to the handler.

If the key is a symbol it will match catches of the same symbol or of #t.

If there is no handler at all, an error is signaled.

procedure: error message args ...

Raise an error with key misc-error and a message constructed by displaying msg and writing args. This normally prints a stack trace, and brings you back to the top level, or exits kawa if you are not running interactively. This procedure is part of SRFI-23, and other Scheme implementations.

Procedure: primitive-throw exception

Throws the exception, which must be an instance of a sub-class of <java.lang.Throwable>.

Syntax: try-finally body handler

Evaluate body, and return its result. However, before it returns, evaluate handler. Even if body returns abnormally (by throwing an exception), handler is evaluated.

(This is implemented just like Java’s try-finally.)

Syntax: try-catch body handler ...

Evaluate body, in the context of the given handler specifications. Each handler has the form:

var type exp ...

If an exception is thrown in body, the first handler is selected such that the thrown exception is an instance of the handler’s type. If no handler is selected, the exception is propagated through the dynamic execution context until a matching handler is found. (If no matching handler is found, then an error message is printed, and the computation terminated.)

Once a handler is selected, the var is bound to the thrown exception, and the exp in the handler are executed. The result of the try-catch is the result of body if no exception is thrown, or the value of the last exp in the selected handler if an exception is thrown.

(This is implemented just like Java’s try-catch.)

Procedure: dynamic-wind in-guard thunk out-guard

All three arguments must be 0-argument procedures. First calls in-guard, then thunk, then out-guard. The result of the expression is that of thunk. If thunk is exited abnormally (by throwing an exception or invoking a continuation), out-guard is called.

If the continuation of the dynamic-wind is re-entered (which is not yet possible in Kawa), the in-guard is called again.

This function was added in R5RS.