[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Proposal for exception handling




I would like to offer a counter proposal as a reponse to Dan, Chris,
and Kent's proposal for exception handling.  An HTML version of their
proposal is available as

  http://www.cs.indiana.edu/scheme-repository/doc.proposals.exceptions.html.

My proposal is a response to their's; read their's first.

What I am proposing is similar to the exception system used in
Scheme 48.  I think it more in keeping with the current Scheme report
than the Indiana proposal, and it has the advantage of ensuring that
exception handlers are dynamically bound.  It also reduces the need
for the with-handlers derived form, which I have left out.

[General dig at the RRRS authors: the system-exception? procedure
described below is required if programs are to be able distinguish
between their and the system's exceptions.  If Scheme included some
way for programs to define new opaque types it would not be needed.]

                                     -Richard Kelsey


As in the original proposal, exceptions are raised by the system when
errors or other difficulties occur, or explicitly by using the RAISE
procedure.  Exceptions can be any object, although exceptions raised
by the system are distinct from all other Scheme objects. 


The following three procedures are supported:


(raise exception)                                              procedure

Invokes the current exception handler with exception.


(with-exception-handler handler thunk)                         procedure

The handler is installed and the thunk is called with no arguments.
When the thunk returns the previous handler is restored.  The return
values are those of the thunk.

If an exception is raised during the execution of the thunk, the
handler is called with two arguments: the exception to be handled and
a thunk that can be called if the handler decides to decline handling
the exception.  The continuation to a call to a handler is that
of the call to raise.  Raising an exception does not change the
currently installed handler.


(system-exception? exception)                                  procedure

Returns #t if the exception is one raised by the system and #f otherwise.



A simple implementation:

(define exception-handlers                  ; list of exception handlers
  (list (lambda (exception fail)
          (error "unhandled exception" exception)))) ; can't call RAISE here

(define (raise exception)
  (let loop ((hs exception-handlers))
    ((car hs) exception (lambda () (loop (cdr hs))))))

(define (with-handler handler thunk)
  (let ((old exception-handlers)
	(new (cons handler exception-handlers)))
    (dynamic-wind (lambda ()
                    (set! exception-handlers new))
                  thunk
                  (lambda ()
                    (set! exception-handlers old)))))