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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
<html><head><title>XLISP catch, throw</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>catch, throw</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, xljump.c</nobr></td>
</tr>
</tbody></table></p>
<h2>Syntax</h2>
<p><div class="box">
<dl>
<dt>(<b>catch</b> <i>tag-symbol</i> [<i>expr</i> ... ])</dt>
<dd><i>tag-symbol</i> - an expression that evaluates to a symbol<br>
<i>expr</i> - an optional series of expressions to be evaluated<br>
returns - the value of the last expression the
<a href="throw.htm">throw</a> expression</dd>
</dl>
<dl>
<dt>(<b>throw</b> <i>tag-symbol</i> [<i>expr</i>])</dt>
<dd><i>tag-symbol</i> - an expression that evaluates to a symbol<br>
<i>expr</i> - an optional expression to be returned<br>
returns - never returns</dd>
</dl>
</div></p>
<h2>Description</h2>
<p>The 'catch' and 'throw' special forms allow for non-local exits and traps
without going through the intermediate evaluations and function returns:</p>
<pre class="example">
(catch <font color="#0000CC">tag-symbol</font>
[<font color="#0000CC">expr</font> <font color="#008844">...</font>]
(throw <font color="#0000CC">tag-symbol</font> [<font color="#0000CC">expr</font>]))
</pre>
<p>If there is a 'catch' for a '<nobr>tag-symbol</nobr>' that has no 'throw'
performed to it, 'catch' returns the value returned from 'expr':</p>
<pre class="example">
> (catch 'mytag
(+ 1 (+ 2 3)))
6
</pre>
<p>If there is no 'expr', <nobr><a href="nil.htm">NIL</a> is</nobr>
returned:</p>
<pre class="example">
> (catch 'mytag)
NIL
</pre>
<p>The 'expr' in 'throw' specifies what value is to be returned by the
corresponding 'catch':</p>
<pre class="example">
> (catch 'mytag
(+ 1 (throw 'mytag 55)))
55
</pre>
<p>If there is no 'expr' in 'throw', <a href="nil.htm">NIL</a> is returned
to the corresponding 'catch':</p>
<pre class="example">
> (catch 'mytag
(throw 'mytag))
NIL
</pre>
<p>If more than one 'catch' is set up for the same
'<nobr>tag-symbol</nobr>', the most recently evaluated
'<nobr>tag-symbol</nobr>' will be the one that does the actual catching:</p>
<pre class="example">
> (catch 'mytag
(catch 'mytag
(throw 'mytag))
(print 'hello))
HELLO
</pre>
<p>If a 'throw' is evaluated with no corresponding 'catch', an error is
signalled:</p>
<pre class="example">
> (catch 'mytag
(throw 'foo))
<font color="#AA0000">error: no target for THROW</font>
</pre>
<p></p>
<h2>Examples</h2>
<pre class="example">
(defun in (x)
(if (numberp x) <font color="#008844">; if X is a number</font>
(+ x x) <font color="#008844">; then double X</font>
(throw 'math 42))) <font color="#008844">; else throw 42</font>
(defun out (x)
(princ "<")
(princ (* (in x) 2)) <font color="#008844">; double via multiply</font>
(princ ">")
"there")
(defun main (x)
(catch 'math (out x))) <font color="#008844">; catch the throw from IN</font>
> (in 5)
10 <font color="#008844">; return value</font>
> (out 5)
<20> <font color="#008844">; screen output of PRINC</font>
"there" <font color="#008844">; return value</font>
> (main 5)
<20> <font color="#008844">; screen output of PRINC</font>
"there" <font color="#008844">; return value</font>
> (main 'a)
< <font color="#008844">; screen output of PRINC</font>
42 <font color="#008844">; return value</font>
</pre>
<p>See <nobr><a href="addition.htm"> + </a></nobr>,
<nobr><a href="multiplication.htm"> * </a></nobr>,
<a href="defun.htm">defun</a>,
<nobr><a href="if.htm"> if </a></nobr>,
<a href="numberp.htm">numberp</a>,
<a href="princ.htm">princ</a>.</p>
<p><div class="box">
<p><b>Note:</b> 'catch' and 'throw' accept not only symbols as
'<nobr>tag-symbol</nobr>', but if a '<nobr>tag-symbol</nobr>' cannot be
compared via <a href="eql.htm">eql</a>, an error is signalled:</p>
<pre class="example">
> (catch "mytag"
(throw "mytag"))
<font color="#AA0000">error: no target for THROW</font>
</pre>
<p>This was reproduced with <nobr>Nyquist 3.03</nobr> in <nobr>December
2010</nobr>.</p>
</div></p>
<p>See also:</p>
<ul>
<li><nobr>Contents → <a href="../manual/contents.htm#control-constructs">Control Constructs</a></nobr></li>
</ul>
<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>
|