[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
In a posting to comp.lang.scheme (**) Henrik Tidefelt pointed out that
the following doesn't work properly using the reference implementation.
It loops indefinitely instead of calling the initial exception handler.
(#f (display "Caught exception.")))
(#f (raise ball)))
When trying to fix this I noticed that the reference implementation doesn't
implement the SRFI as specified. It goes to some effort to call a handler
in the dynamic environment of the WITH-HANDLER call that installed it
instead of using the dynamic environment of the call to RAISE, which is
what is required. It's a bug in the dynamic environment manipulation that
causes the behavior that Tidefelt noticed.
I believe that the following code is a correct implemenation of SRFI 34.
It works for the examples in the SRFI and Tidefelt's code, as well as some
tests that check that handlers are called with the correct dynamic
(list (lambda (condition)
(error "unhandled exception" condition))))
(define (with-exception-handler handler thunk)
(with-exception-handlers (cons handler *current-exception-handlers*)
(define (with-exception-handlers new-handlers thunk)
(let ((previous-handlers *current-exception-handlers*))
(set! *current-exception-handlers* new-handlers))
(set! *current-exception-handlers* previous-handlers)))))
(define (raise obj)
(let ((handlers *current-exception-handlers*))
(with-exception-handlers (cdr handlers)
((car handlers) obj)
(error "handler returned"
((guard (var clause ...) e1 e2 ...)
(let ((var condition)) ; clauses may SET! var
(guard-aux (handler-k (lambda ()
(lambda () e1 e2 ...)
(guard-k (lambda ()
(apply values args)))))))))))))
... as in the original ...)