unwind-protect
type: special form (fsubr) location: built-in source file: xlcont.c Common LISP compatible: yes supported on: all machines
SYNTAX
(unwind-protect <protect-form> <clean-up-form> ... ) <protect-form> - a form that is to be protected <clean-up-form> - a sequence forms to execute after <protect-form>
DESCRIPTION
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, THROW, RETURN and GO. The <clean-up-form> will be executed in all cases - when there is an exit from <protect-form> and when the form does not have exit. UNWIND-PROTECT will return the result from the <protect-form>, not from the <clean-up-form>s. Errors or exits that occur in the <clean-up-form> are not protected. It is possible to trap these with another UNWIND-PROTECT.
EXAMPLES
(unwind-protect ; (+ 2 2) ; protected form (print "an exit")) ; clean up form ; prints "an exit" ; returns 4 ; (nodebug) ; 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" ; (nodebug) ; 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"