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

6.16.4 Low-level exception mechanism

This layer provides SRFI-18 compatible simple exception mechanism. You can override the behavior of higher-level constructs such as with-error-handler by using with-exception-handler.

Note that it is a double-edged sword. You'll get a freedom to construct your own exception handling semantics, but the Gauche system won't save if something goes wrong. Use these primitives when you want to customize the system's higher-level semantics or you are porting from other SRFI-18 code.

Function: current-exception-handler
[SRFI-18] Returns the current exception handler.

Function: raise exception
[SRFI-18] Invokes the current exception handler with one argument, exception. exception can be any Scheme object; it doesn't need to be an instance of <exception> class. Combined with with-exception-handler you can give your own semantics to the exception. raise returns whatever the handler returns.

Function: with-exception-handler handler thunk
[SRFI-18] A procedure handler must take one argument. This procedure sets handler to the current exception handler and calls thunk.

Generally, if you want to handle non-continuable exception such as errors using this low-level mechanism, you have to transfer the control from the handler explicitly (See the explanation of with-error-handler above). raise detects if the handler returns on the non-continuable exceptions and reports an error using the default error handler mechanism, but it is just a safety net.

Note also that handler is called in the same dynamic environment of raise. So if you raise an exception inside handler, it is captured by handler again. It is the programmer's responsibility to propagate the exception handling to the "outer" exception handlers.

The behavior of those procedures can be explained in the following conceptual Scheme code.

 
;; Conceptual implementation of low-level exception mechanism.
;; Suppose %xh is a list of exception handlers

(define (current-exception-handler) (car %xh))

(define (raise exn)
  (receive r ((car %xh) exn)
    (when (uncontinuable-exception? exn)
      (set! %xh (cdr %xh))
      (raise (make-error "returned from uncontinuable exception")))
    (apply values r)))

(define (with-exception-handler handler thunk)
  (let ((prev %xh))
    (dynamic-wind
      (lambda () (set! %xh (cons handler)))
      thunk
      (lambda () (set! %xh prev)))))


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

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