A history of Lisp error systems

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.

For anyone interested in Lisp, this history of how thinking evolved regarding error systems is a very good read.

A key capability provided by Common Lisp is the fact that, at the most primitive level, handling can be done in the dynamic context of the signaler, while certain very critical dynamic state information is still available that would be lost if a stack unwind happened before running the handler.

This capability is reflected in the ability of the operator handler-bind to take control of a computation before any transfer of control occurs. Note that the Common Lisp operator handler-case, which is more analogous to facilities offered in other languages, does not allow programmer-supplied code to run until after the transfer of control; this is useful for some simple situations, but is less powerful.

Consider a code fragment such as:

(handler-case (main-action)
(error (c) (other-action)))

In this example, the expression (other-action) will run after unwinding from wherever in (main-action) signaled the error, regardless of how deep into main-action that signaling occured.

By contrast, handler-bind takes control inside the dynamic context of the call to SIGNAL, and so is capable of accessing restarts that are dynamically between the call to SIGNAL and the use of HANDLER-BIND. Consider this example:

(with-simple-restart (foo “Outer foo.”)
(handler-case (with-simple-restart (foo “Inner foo.”)
(error “Lossage.”))
(error (c) (invoke-restart ‘foo))))

In the above, the outer FOO restart will be selected, as contrasted with the following, where the inner FOO restart will be selected:

(with-simple-restart (foo “Outer foo.”)
(handler-bind ((error #'(lambda (c) (invoke-restart ‘foo))))
(with-simple-restart (foo “Inner foo.”)
(error “Lossage.”))))

This is important because error handling tends to want to make use of all available restarts, but especially those that are in that code region that HANDLER-BIND can see but HANDLER-CASE cannot.

Post external references

  1. 1
    http://www.nhplace.com/kent/Papers/Condition-Handling-2001.html
Source