1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
<html><head><title>XLISP unwind-protect</title>
<link rel="stylesheet" type="text/css" href="reference.css">
</head>
<body>
<a href="../start.htm">Nyquist / XLISP 2.0</a> -
<a href="../manual/contents.htm">Contents</a> |
<a href="../tutorials/tutorials.htm">Tutorials</a> |
<a href="../examples/examples.htm">Examples</a> |
<a href="reference-index.htm">Reference</a>
<hr>
<h1>unwind-protect</h1>
<hr>
<p><table cellpadding="0" cellspacing="0" style="margin-left:10px"><tbody>
<tr valign="top">
<td><nobr>Type:</nobr></td>
<td><nobr> - </nobr></td>
<td width="100%"><nobr>special form (fsubr)</nobr></td>
</tr>
<tr valign="top">
<td><nobr>Source:</nobr></td>
<td><nobr> - </nobr></td>
<td width="100%"><nobr>xlcont.c</nobr></td>
</tr>
</tbody></table></p>
<h2>Syntax</h2>
<dl>
<dt>(unwind-protect <i>protect-form clean-up-form</i> ... )</dt>
<dd><i>protect-form</i> - a form that is to be protected<br>
<i>clean-up-form</i> - a sequence forms to execute after <i>protect-form</i><br>
returns - the value of <i>protect-form</i></dd>
</dl>
<h2>Description</h2>
<p> 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, <nobr><a href="throw.htm">throw</a> ,</nobr> <a
href="return.htm">return</a> and <a
href="go.htm">go</a>. 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'.</p>
<p><div class="box">
<p><b>Note:</b> 'unwind-protext' will not protect against errors signalled
by <nobr>built-in</nobr> functions if
<a href="global-breakenable.htm">*breakenable*</a> is <nobr>not
<a href="nil.htm">NIL</a></nobr></nobr>.</p>
</div></p>
<h2>Examples</h2>
<pre class="example">
(unwind-protect
(+ 2 2) <font color="#008844">; protected form</font>
(print "an exit")) <font color="#008844">; clean up form</font>
<font color="#008844">; prints "an exit"</font>
<font color="#008844">; returns 4</font>
</pre>
<pre class="example">
(setq *breakenable* nil) <font color="#008844">; to turn off break loop traps</font>
(unwind-protect
(+ 1 "2") <font color="#008844">; protected form</font>
(print "something happened")) <font color="#008844">; clean up form</font>
<font color="#008844">; error: bad argument type - "2"</font>
<font color="#008844">; prints "something happened"</font>
</pre>
<pre class="example">
(catch 'mytag
(unwind-protect
(throw 'mytag) <font color="#008844">; protected form</font>
(print "an exit"))) <font color="#008844">; clean up form</font>
<font color="#008844">; prints "an exit"</font>
</pre>
<pre class="example">
(setq *breakenable* nil) <font color="#008844">; to turn off break loop traps</font>
(unwind-protect
(throw 'notag) <font color="#008844">; protected form</font>
(print "an exit")) <font color="#008844">; clean up form</font>
<font color="#008844">; error: no target for THROW</font>
<font color="#008844">; prints "an exit"</font>
</pre>
<pre class="example">
(prog () (print "start")
(unwind-protect
(go end) <font color="#008844">; protected form</font>
(print "an exit")) <font color="#008844">; clean-up form</font>
end (print "end")) <font color="#008844">; prints "start"</font>
<font color="#008844">; prints "an exit"</font>
<font color="#008844">; prints "end"</font>
</pre>
<pre class="example">
(prog () (print "start")
(unwind-protect
(return "I'm done") <font color="#008844">; protected form</font>
(print "but first")) <font color="#008844">; clean-up form</font>
(print "won't get here")) <font color="#008844">; prints "start"</font>
<font color="#008844">; prints "but first"</font>
<font color="#008844">; returns "I'm done"</font>
</pre>
<p>See the
<a href="../manual/xlisp-man-019.htm#unwind-protect">unwind-protect</a>
special form in the <nobr>XLISP 2.0</nobr> manual.</p>
<p><nobr> <a href="#top">Back to Top</nobr></a></p>
<hr>
<a href="../start.htm">Nyquist / XLISP 2.0</a> -
<a href="../manual/contents.htm">Contents</a> |
<a href="../tutorials/tutorials.htm">Tutorials</a> |
<a href="../examples/examples.htm">Examples</a> |
<a href="reference-index.htm">Reference</a>
</body></html>
|