[plt-scheme] restart exception handling?
At Thu, 10 Jul 2008 14:35:57 -0700, YC wrote:
> Is it possible to handle exceptions like Common Lisp, specifically
> restarts?
Not in general. Aside from `exn:break', built-in exceptions are raised
in a non-continuable way.
> Scenario: undefined variables - handling by defining the variable, and
> continue past the exception.
In this specific case, you could adjust the compilation of top-level
variable access by defining `#%top'. Here's one way:
> (define (handle-undefined exn)
(abort-current-continuation
(default-continuation-prompt-tag)
(lambda () (letrec ([x x]) x))))
> (require (only-in scheme/base [#%top orig-top]))
> (define-syntax-rule (#%top . id)
(call-with-continuation-prompt
(lambda ()
(call-with-exception-handler
(orig-top . handle-undefined)
(lambda () (orig-top . id))))))
> undef
made-up
> (define def 5)
> def
5
Installing a prompt and exception handler for every top-level lookup is
expensive, though --- about 50 times as expensive as a normal top-level
lookup (when the variable is defined) in my test.
> I found in manual that exceptions are marked with barriers and may not be
> crossable, so not sure if the above is achievable.
That's not quite the problem. It's more that the exception handler is
required to escape, and also that the exception for an undefined
variable doesn't provide a place to escape back to.
Matthew