398 lines
8.3 KiB
HTML
398 lines
8.3 KiB
HTML
|
<!DOCTYPE HTML>
|
||
|
<HTML LANG="en-us"
|
||
|
><HEAD
|
||
|
><TITLE
|
||
|
>throw | Common Lisp Nova Spec</TITLE
|
||
|
><META CHARSET="US-ASCII"
|
||
|
><LINK REL="canonical" HREF="f_throw.html"
|
||
|
><LINK REL="next" HREF="f_unwind-protect.html" TYPE="text/html" TITLE="unwind-protect"
|
||
|
><LINK REL="prev" HREF="f_tagbody.html" TYPE="text/html" TITLE="tagbody"
|
||
|
><LINK REL="up" HREF="5_3_Data_and_Control_Flow_Dictionary.html" TYPE="text/html" TITLE="5.3 Data and Control Flow Dictionary"
|
||
|
><LINK REL="start" HREF="index.html" TYPE="text/html" TITLE="Common Lisp Nova Spec"
|
||
|
><META NAME="VIEWPORT" CONTENT="width=device-width, initial-scale=1.0"
|
||
|
><LINK REL="STYLESHEET" HREF="dpans.css%3F3909942064.css"
|
||
|
><SCRIPT SRC="dpans.js%3F3909942064"
|
||
|
></SCRIPT
|
||
|
><SCRIPT SRC="apropos.js%3F3909942064"
|
||
|
></SCRIPT
|
||
|
></HEAD
|
||
|
><BODY
|
||
|
><DIV
|
||
|
><DIV CLASS="topnav"
|
||
|
><DIV CLASS="breadcrumb"
|
||
|
><SPAN CLASS="breadcrumb-item"
|
||
|
><A HREF="index.html"
|
||
|
>Common Lisp Nova Spec</A
|
||
|
></SPAN
|
||
|
> <SPAN CLASS="breadcrumb-item"
|
||
|
>→ <A HREF="5_Data_and_Control_Flow.html"
|
||
|
>5. Data and Control Flow</A
|
||
|
></SPAN
|
||
|
> <SPAN CLASS="breadcrumb-item"
|
||
|
>→ <A HREF="5_3_Data_and_Control_Flow_Dictionary.html"
|
||
|
>5.3 Data and Control Flow Dictionary</A
|
||
|
></SPAN
|
||
|
> <SPAN CLASS="breadcrumb-item"
|
||
|
>→ <A HREF="f_throw.html"
|
||
|
>throw</A
|
||
|
></SPAN
|
||
|
></DIV
|
||
|
><DIV CLASS="apropos"
|
||
|
><DIV CLASS="apropos-io"
|
||
|
><A HREF="f_tagbody.html" CLASS="prev"
|
||
|
>←</A
|
||
|
><SPAN ID="apropos-label"
|
||
|
>Apropos </SPAN
|
||
|
><INPUT ID="apropos" AUTOFOCUS="AUTOFOCUS" PLACEHOLDER="Type here to search" ONINPUT="aproposInput(this);" ONKEYUP="aproposKeyup(event);" ONCHANGE="aproposChange(this);" ONFOCUS="aproposFocus(this);" ONFOCUSOUT="aproposFocusout(this);"
|
||
|
><A HREF="f_unwind-protect.html" CLASS="next"
|
||
|
>→</A
|
||
|
></DIV
|
||
|
><DIV ID="apropos-res"
|
||
|
></DIV
|
||
|
></DIV
|
||
|
></DIV
|
||
|
><DIV CLASS="matter"
|
||
|
><DIV CLASS="com"
|
||
|
><DIV CLASS="begincom"
|
||
|
><HR
|
||
|
><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0"
|
||
|
><TR
|
||
|
><TD ALIGN="LEFT" VALIGN="BASELINE" WIDTH="100%" CLASS="name"
|
||
|
><SPAN CLASS="idx" DATA-KIND="idxref" DATA-TERM="throw"
|
||
|
></SPAN
|
||
|
><B
|
||
|
>throw</B
|
||
|
></TD
|
||
|
><TD ALIGN="RIGHT" VALIGN="BASELINE" WIDTH="0" NOWRAP="NOWRAP" CLASS="ftype"
|
||
|
><I
|
||
|
>Special</I
|
||
|
> <I
|
||
|
>Operator</I
|
||
|
></TD
|
||
|
></TR
|
||
|
></TABLE
|
||
|
><HR
|
||
|
></DIV
|
||
|
><UL CLASS="subtoc"
|
||
|
></UL
|
||
|
><DL
|
||
|
><DT
|
||
|
><B
|
||
|
>Syntax</B
|
||
|
></DT
|
||
|
><DD
|
||
|
><P CLASS="j"
|
||
|
><B
|
||
|
>throw</B
|
||
|
> <SPAN CLASS="cmssi"
|
||
|
>tag</SPAN
|
||
|
> <SPAN CLASS="cmssi"
|
||
|
>result-form</SPAN
|
||
|
> <SPAN CLASS="arrow"
|
||
|
>→</SPAN
|
||
|
>|</P
|
||
|
></DD
|
||
|
><DT
|
||
|
><B
|
||
|
>Arguments and Values</B
|
||
|
></DT
|
||
|
><DD
|
||
|
><P CLASS="j"
|
||
|
><VAR CLASS="param"
|
||
|
>tag</VAR
|
||
|
> — a <A HREF="26_1_Glossary.html#catch_tag"
|
||
|
><EM CLASS="term"
|
||
|
>catch tag</EM
|
||
|
></A
|
||
|
>; evaluated. </P
|
||
|
><P CLASS="j"
|
||
|
><VAR CLASS="param"
|
||
|
>result-form</VAR
|
||
|
> — a <A HREF="26_1_Glossary.html#form"
|
||
|
><EM CLASS="term"
|
||
|
>form</EM
|
||
|
></A
|
||
|
>; evaluated as described below.</P
|
||
|
></DD
|
||
|
><DT
|
||
|
><B
|
||
|
>Description</B
|
||
|
></DT
|
||
|
><DD
|
||
|
><P CLASS="j"
|
||
|
><A HREF="f_throw.html" CLASS="specref"
|
||
|
><B
|
||
|
>throw</B
|
||
|
></A
|
||
|
> causes a non-local control transfer to a <A HREF="f_catch.html" CLASS="specref"
|
||
|
><B
|
||
|
>catch</B
|
||
|
></A
|
||
|
> whose tag is <A HREF="f_eq.html" CLASS="funref"
|
||
|
><B
|
||
|
>eq</B
|
||
|
></A
|
||
|
> to <VAR CLASS="param"
|
||
|
>tag</VAR
|
||
|
>. </P
|
||
|
><P CLASS="j"
|
||
|
><VAR CLASS="param"
|
||
|
>Tag</VAR
|
||
|
> is evaluated first to produce an <A HREF="26_1_Glossary.html#object"
|
||
|
><EM CLASS="term"
|
||
|
>object</EM
|
||
|
></A
|
||
|
> called the throw tag; then <VAR CLASS="param"
|
||
|
>result-form</VAR
|
||
|
> is evaluated, and its results are saved. If the <VAR CLASS="param"
|
||
|
>result-form</VAR
|
||
|
> produces multiple values, then all the values are saved. The most recent outstanding <A HREF="f_catch.html" CLASS="specref"
|
||
|
><B
|
||
|
>catch</B
|
||
|
></A
|
||
|
> whose <VAR CLASS="param"
|
||
|
>tag</VAR
|
||
|
> is <A HREF="f_eq.html" CLASS="funref"
|
||
|
><B
|
||
|
>eq</B
|
||
|
></A
|
||
|
> to the throw tag is exited; the saved results are returned as the value or values of <A HREF="f_catch.html" CLASS="specref"
|
||
|
><B
|
||
|
>catch</B
|
||
|
></A
|
||
|
>. </P
|
||
|
><P CLASS="j"
|
||
|
>The transfer of control initiated by <A HREF="f_throw.html" CLASS="specref"
|
||
|
><B
|
||
|
>throw</B
|
||
|
></A
|
||
|
> is performed as described in <A HREF="5_2_Transfer_of_Control_to_an_Exit_Point.html#sec_5_2" CLASS="secref"
|
||
|
><SPAN CLASS="cmr"
|
||
|
>Section</SPAN
|
||
|
> <SPAN CLASS="cmr"
|
||
|
>5.2</SPAN
|
||
|
> <SPAN CLASS="cmr"
|
||
|
>(Transfer</SPAN
|
||
|
> <SPAN CLASS="cmr"
|
||
|
>of</SPAN
|
||
|
> <SPAN CLASS="cmr"
|
||
|
>Control</SPAN
|
||
|
> <SPAN CLASS="cmr"
|
||
|
>to</SPAN
|
||
|
> <SPAN CLASS="cmr"
|
||
|
>an</SPAN
|
||
|
> <SPAN CLASS="cmr"
|
||
|
>Exit</SPAN
|
||
|
> <SPAN CLASS="cmr"
|
||
|
>Point)</SPAN
|
||
|
></A
|
||
|
>.</P
|
||
|
></DD
|
||
|
><DT
|
||
|
><B
|
||
|
>Examples</B
|
||
|
></DT
|
||
|
><DD
|
||
|
><PRE CLASS="screen"
|
||
|
>(catch 'result
|
||
|
(setq i 0 j 0)
|
||
|
(loop (incf j 3) (incf i)
|
||
|
(if (= i 3) (throw 'result (values i j))))) <SPAN CLASS="cmsy"
|
||
|
><SPAN CLASS="arrow"
|
||
|
>→</SPAN
|
||
|
></SPAN
|
||
|
> 3, 9</PRE
|
||
|
><PRE CLASS="screen"
|
||
|
>(catch nil
|
||
|
(unwind-protect (throw nil 1)
|
||
|
(throw nil 2))) <SPAN CLASS="cmsy"
|
||
|
><SPAN CLASS="arrow"
|
||
|
>→</SPAN
|
||
|
></SPAN
|
||
|
> 2</PRE
|
||
|
><P CLASS="j"
|
||
|
>The consequences of the following are undefined because the <A HREF="f_catch.html" CLASS="specref"
|
||
|
><B
|
||
|
>catch</B
|
||
|
></A
|
||
|
> of <CODE CLASS="f"
|
||
|
>b</CODE
|
||
|
> is passed over by the first <A HREF="f_throw.html" CLASS="specref"
|
||
|
><B
|
||
|
>throw</B
|
||
|
></A
|
||
|
>, hence portable programs must assume that its <A HREF="26_1_Glossary.html#dynamic_extent"
|
||
|
><EM CLASS="term"
|
||
|
>dynamic extent</EM
|
||
|
></A
|
||
|
> is terminated. The <A HREF="26_1_Glossary.html#binding"
|
||
|
><EM CLASS="term"
|
||
|
>binding</EM
|
||
|
></A
|
||
|
> of the <A HREF="26_1_Glossary.html#catch_tag"
|
||
|
><EM CLASS="term"
|
||
|
>catch tag</EM
|
||
|
></A
|
||
|
> is not yet <A HREF="26_1_Glossary.html#disestablish"
|
||
|
><EM CLASS="term"
|
||
|
>disestablished</EM
|
||
|
></A
|
||
|
> and therefore it is the target of the second <A HREF="f_throw.html" CLASS="specref"
|
||
|
><B
|
||
|
>throw</B
|
||
|
></A
|
||
|
>.</P
|
||
|
><PRE CLASS="screen"
|
||
|
>(catch 'a
|
||
|
(catch 'b
|
||
|
(unwind-protect (throw 'a 1)
|
||
|
(throw 'b 2))))</PRE
|
||
|
><P CLASS="j"
|
||
|
>The following prints “<CODE CLASS="f"
|
||
|
>The inner catch returns :SECOND-THROW</CODE
|
||
|
>” and then returns <CODE CLASS="f"
|
||
|
>:outer-catch</CODE
|
||
|
>.</P
|
||
|
><PRE CLASS="screen"
|
||
|
> (catch 'foo
|
||
|
(format t "The inner catch returns ~s.~%"
|
||
|
(catch 'foo
|
||
|
(unwind-protect (throw 'foo :first-throw)
|
||
|
(throw 'foo :second-throw))))
|
||
|
:outer-catch)
|
||
|
<SPAN CLASS="cmr"
|
||
|
>⊳</SPAN
|
||
|
> The inner catch returns :SECOND-THROW
|
||
|
<SPAN CLASS="cmsy"
|
||
|
><SPAN CLASS="arrow"
|
||
|
>→</SPAN
|
||
|
></SPAN
|
||
|
> :OUTER-CATCH</PRE
|
||
|
></DD
|
||
|
><DT
|
||
|
><B
|
||
|
>Exceptional Situations</B
|
||
|
></DT
|
||
|
><DD
|
||
|
><P CLASS="j"
|
||
|
>If there is no outstanding <A HREF="26_1_Glossary.html#catch_tag"
|
||
|
><EM CLASS="term"
|
||
|
>catch tag</EM
|
||
|
></A
|
||
|
> that matches the throw tag, no unwinding of the stack is performed, and an error of <A HREF="26_1_Glossary.html#type"
|
||
|
><EM CLASS="term"
|
||
|
>type</EM
|
||
|
></A
|
||
|
> <A HREF="t_control-error.html" CLASS="typeref"
|
||
|
><B
|
||
|
>control-error</B
|
||
|
></A
|
||
|
> is signaled. When the error is signaled, the <A HREF="26_1_Glossary.html#dynamic_environment"
|
||
|
><EM CLASS="term"
|
||
|
>dynamic environment</EM
|
||
|
></A
|
||
|
> is that which was in force at the point of the <A HREF="f_throw.html" CLASS="specref"
|
||
|
><B
|
||
|
>throw</B
|
||
|
></A
|
||
|
>.</P
|
||
|
></DD
|
||
|
><DT
|
||
|
><B
|
||
|
>See Also</B
|
||
|
></DT
|
||
|
><DD
|
||
|
><P CLASS="j"
|
||
|
><A HREF="f_block.html" CLASS="specref"
|
||
|
><B
|
||
|
>block</B
|
||
|
></A
|
||
|
>, <A HREF="f_catch.html" CLASS="specref"
|
||
|
><B
|
||
|
>catch</B
|
||
|
></A
|
||
|
>, <A HREF="f_return-from.html" CLASS="specref"
|
||
|
><B
|
||
|
>return-from</B
|
||
|
></A
|
||
|
>, <A HREF="f_unwind-protect.html" CLASS="specref"
|
||
|
><B
|
||
|
>unwind-protect</B
|
||
|
></A
|
||
|
>, <A HREF="3_1_Evaluation.html#sec_3_1" CLASS="secref"
|
||
|
><SPAN CLASS="cmr"
|
||
|
>Section</SPAN
|
||
|
> <SPAN CLASS="cmr"
|
||
|
>3.1</SPAN
|
||
|
> <SPAN CLASS="cmr"
|
||
|
>(Evaluation)</SPAN
|
||
|
></A
|
||
|
></P
|
||
|
></DD
|
||
|
><DT
|
||
|
><B
|
||
|
>Notes</B
|
||
|
></DT
|
||
|
><DD
|
||
|
><P CLASS="j"
|
||
|
><A HREF="f_catch.html" CLASS="specref"
|
||
|
><B
|
||
|
>catch</B
|
||
|
></A
|
||
|
> and <A HREF="f_throw.html" CLASS="specref"
|
||
|
><B
|
||
|
>throw</B
|
||
|
></A
|
||
|
> are normally used when the <A HREF="26_1_Glossary.html#exit_point"
|
||
|
><EM CLASS="term"
|
||
|
>exit point</EM
|
||
|
></A
|
||
|
> must have <A HREF="26_1_Glossary.html#dynamic_scope"
|
||
|
><EM CLASS="term"
|
||
|
>dynamic scope</EM
|
||
|
></A
|
||
|
> (<I CLASS="i"
|
||
|
><I
|
||
|
>e.g.</I
|
||
|
></I
|
||
|
>, the <A HREF="f_throw.html" CLASS="specref"
|
||
|
><B
|
||
|
>throw</B
|
||
|
></A
|
||
|
> is not lexically enclosed by the <A HREF="f_catch.html" CLASS="specref"
|
||
|
><B
|
||
|
>catch</B
|
||
|
></A
|
||
|
>), while <A HREF="f_block.html" CLASS="specref"
|
||
|
><B
|
||
|
>block</B
|
||
|
></A
|
||
|
> and <A HREF="f_return.html" CLASS="macref"
|
||
|
><B
|
||
|
>return</B
|
||
|
></A
|
||
|
> are used when <A HREF="26_1_Glossary.html#lexical_scope"
|
||
|
><EM CLASS="term"
|
||
|
>lexical scope</EM
|
||
|
></A
|
||
|
> is sufficient.</P
|
||
|
></DD
|
||
|
></DL
|
||
|
></DIV
|
||
|
></DIV
|
||
|
><DIV CLASS="footer"
|
||
|
><DIV CLASS="btmnav"
|
||
|
><A HREF="f_tagbody.html" CLASS="prev"
|
||
|
>←</A
|
||
|
><A HREF="f_unwind-protect.html" CLASS="next"
|
||
|
>→</A
|
||
|
></DIV
|
||
|
><DIV CLASS="trail"
|
||
|
>Conversion to HTML copyright 2023 by Gilbert Baumann</DIV
|
||
|
></DIV
|
||
|
></DIV
|
||
|
><SCRIPT
|
||
|
>domReady();</SCRIPT
|
||
|
></BODY
|
||
|
></HTML
|
||
|
>
|