GNU Info

Info Node: (librep.info)Cleanup Forms

(librep.info)Cleanup Forms


Next: Errors Prev: Function Exits Up: Non-Local Exits
Enter node , (file) or (file)node

Cleanup Forms
.............

   It is sometimes necessary ensure that a certain form is _always_
evaluated, even when a non-local exit would normally bypass that form.
The `unwind-protect' special form is used in this case.

 - Macro: unwind-protect body-form cleanup-forms...
     The BODY-FORM is evaluated, if it exits normally the CLEANUP-FORMS
     are evaluated sequentially then the value which the BODY-FORM
     returned becomes the value of the `unwind-protect' form. If the
     BODY-FORM exits abnormally though (i.e. a non-local exit happened)
     the CLEANUP-FORMS are evaluated anyway and the non-local exit
     continues.

   One use of this is to ensure that an opened file is always closed,
for example,

     (catch 'foo
       (unwind-protect
           (let
               ((temporary-file (open-file (make-temp-name) 'write)))
             ;; Use `temporary-file'
             (write temporary-file "A test\n")
             ;; Now force a non-local exit
             (throw 'foo))
         ;; This is the CLEANUP-FORM it will _always_
         ;; be evaluated, despite the `throw'.
         (close temporary-file)))
         => ()


automatically generated by info2www version 1.2.2.9