Debian Codegraph @

docsrc/xlisp/xlisp-doc/reference/unwind-protect.htm

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