Next: , Previous: Catch, Up: Exceptions Throw Handlers

It's sometimes useful to be able to intercept an exception that is being thrown, but without changing where in the dynamic context that exception will eventually be caught. This could be to clean up some related state or to pass information about the exception to a debugger, for example. The with-throw-handler procedure provides a way to do this.

— Scheme Procedure: with-throw-handler key thunk handler
— C Function: scm_with_throw_handler (key, thunk, handler)





The behaviour of a throw handler is very similar to that of a catch expression's optional pre-unwind handler. In particular, a throw handler's handler procedure is invoked in the exact dynamic context of the throw expression, just as a pre-unwind handler is. with-throw-handler may be seen as a half-catch: it does everything that a catch would do until the point where catch would start unwinding the stack and dynamic context, but then it rethrows to the next innermost catch or throw handler instead.