[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.16.2 Handling errors

Function: with-error-handler handler thunk
Makes handler the active error handler and executes thunk. If thunk returns normally, the result(s) will be returned. If an error is signalled during execution of thunk, handler is called with one argument, an exception object representing the error, with the continuation of with-error-handler. That is, with-error-handler returns whatever value(s) handler returns.

If handler signals an error, it will be handled by the handler installed when with-error-handler called.

The dynamic environment where handler is executed is the same as the error occurs. If dynamic-wind is used in thunk, its after method is called after handler has returned, and before with-error-handler returns.

The behavior of with-error-handler can be described in the form of Scheme code shown below, using the low-level mechanism (See section 6.16.4 Low-level exception mechanism). Note that the actual function is built in VM, using lighter mechanisms (similar to "one-shot continuation", 1CC).

 
;; conceptual implementation of with-error-handler
(define (with-error-handler handler thunk)
  (call/cc
    (lambda (cont)
      (let* ((prev-handler (current-exception-handler))
        (with-exception-handler
          (lambda (exn)
            (if (error? exn)
                (with-exception-handler
                  (lambda (err) (prev-handler err))
                  (lambda () (call-with-values (handler exn) cont)))
                (prev-handler exn)))
          thunk))))))



This document was generated by Ken Dickey on November, 28 2002 using texi2html