The 'unwind-protect' special form allows the protecting [trapping] of
all forms of exit from the 'protect-form'. The exits that are trapped
include errors,
(unwind-protect (+ 2 2) ; protected form (print "an exit")) ; clean up form ; prints "an exit" ; returns 4
(setq *breakenable* nil) ; to turn off break loop traps (unwind-protect (+ 1 "2") ; protected form (print "something happened")) ; clean up form ; error: bad argument type - "2" ; prints "something happened"
(catch 'mytag (unwind-protect (throw 'mytag) ; protected form (print "an exit"))) ; clean up form ; prints "an exit"
(setq *breakenable* nil) ; to turn off break loop traps (unwind-protect (throw 'notag) ; protected form (print "an exit")) ; clean up form ; error: no target for THROW ; prints "an exit"
(prog () (print "start") (unwind-protect (go end) ; protected form (print "an exit")) ; clean-up form end (print "end")) ; prints "start" ; prints "an exit" ; prints "end"
(prog () (print "start") (unwind-protect (return "I'm done") ; protected form (print "but first")) ; clean-up form (print "won't get here")) ; prints "start" ; prints "but first" ; returns "I'm done"
See the
unwind-protect
special form in the