Re: Returning to top-level on error



Pascal Bourguignon wrote:

Can Lisp be told not to enter the "break loop" when encountering an
error, but instead just return to the top-level?

(ignore-errors (do-something))

This is appropriate for almost any normal circumstance, but isn't quite bulletproof.

ignore-errors is roughly equivalent to a handler-case for conditions
of type cl:error wrapped around the body.  In CL errors are usually
signalled with the error function, which is defined approximately

(defun error (datum &rest arguments)
  (let ((condition (_coerce-to-condition_ frob rest)))
    (signal condition)
    (invoke-debugger condition)))

The _coerce-to-condition_ function implements the hairy way condition
signalling functions interpret their arguments either to be a condition
or to be a designator for a condition.  The condition is signalled, and
if some surrounding handler handles it (by not returning) the debugger
is not called.  But if no surrounding handler handles the condition then
the debugger will be entered.

Here's an example how ignore-errors might not be adequate:

 cl-user(5): (ignore-errors
              (error (make-condition 'simple-error
                       :format-control "Hello, world!"
                       :format-arguments nil)))
 nil
 #<simple-error @ #x100178a5f2>
 cl-user(6): (ignore-errors
              (error (make-condition 'simple-warning
                       :format-control "Hello, world!"
                       :format-arguments nil)))
 Error: Hello, world!
   [condition type: simple-warning]

 Restart actions (select using :continue):
  0: Return to Top Level (an "abort" restart).
  1: Abort entirely from this (lisp) process.

I think Pascal's answer is correct and quite sufficient in normal
circumstances and in the way the condition system is normally used,
but it isn't bulletproof in the face of strangely written
application code.
.