Next: Threads and Namespaces Up: Exceptions and Control Flow Previous: Breaks

# Error Escape Handler

Special control flow for exceptions is performed by an error escape handler that is called by the default exception handler. An error escape handler takes no arguments and must escape from the expression that raised the exception. The error escape handler is obtained or set using the `error-escape-handler` parameter (see section 9.4.1.9).

An error escape handler cannot invoke a full continuation that was created prior to the exception, but it can invoke an escape continuation (see section 8.3).

The error escape handler is normally called directly by an exception handler. To escape from a run-time error, use `raise` (see section 8.1) or `error` (see section 8.2) instead.

If an exception is raised while the error escape handler is executing, an error message is printed using a primitive error printer and a primitive error escape handler is invoked.

In the following example, the error escape handler is set so that errors do not escape from a custom `read`-`eval`-`print` loop:

      (let ([orig (error-escape-handler)])
        (let/ec exit
          (let retry-loop ()
            (let/ec escape
              (error-escape-handler
               (lambda () (escape #f)))
              (let loop ()
                (let ([e (my-read)])
                  (if (eof-object? e)
                      (exit 'done)
                      (let ([v (my-eval e)])
                        (my-print v)
                        (loop))))))
            (retry-loop)))
        (error-escape-handler orig))

See also `read-eval-print-loop` in section 14.1 for a simpler implementation of this example.