XLISP unwind-protect

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference

# unwind-protect

Type: | - | special form (fsubr)
Source: | - | xlcont.c

## 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
  returns - the value of 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-forms'. Errors or exits that occur in the 'clean-up-form' are not protected. It is possible to trap these with another 'unwind-protect'.

Note: 'unwind-protext' will not protect against errors signalled by built-in functions if *breakenable* is not NIL.

## Examples

    (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 XLISP 2.0 manual.

Back to Top

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference