with-exception-handler

[procedure] with-exception-handler handler thunk

SRFI-18, SRFI-21, SRFI-34: a fundamental exception handling mechanism.

From SRFI-34:

Returns the result(s) of invoking thunk. Handler must be a procedure that accepts one argument. It is installed as the current exception handler for the dynamic extent (as determined by dynamic-wind) of the invocation of thunk.

See the discussion in raise about the dynamic environment where handler is called.

[procedure] with-exception-handler handler thunk [#:unwind?=#f] [#:unwind-for-type=#t]

Guile: Establish handler, a procedure of one argument, as the current exception handler during the dynamic extent of invoking thunk.

If raise-exception is called during the dynamic extent of invoking thunk, handler will be invoked on the argument of raise-exception.

By default, exception handlers are non-unwinding. Unless with-exception-handler was invoked with #:unwind? #t, exception handlers are invoked within the continuation of the error, without unwinding the stack.

#:unwind-for-type keyword argument allows users to specify the kind of exception handled by an exception handler; if #t, all exceptions will be handled; if an exception type object, only exceptions of that type will be handled; otherwise if a symbol, only that exceptions with the given exception-kind will be handled.

See also current-exception-handler, guard.