90 lines
6.9 KiB
HTML
90 lines
6.9 KiB
HTML
<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>CLHS: Special Operator THROW</TITLE>
|
|
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
|
|
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
|
|
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
|
|
<LINK REL=TOP HREF="../Front/index.htm">
|
|
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
|
|
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
|
|
<LINK REL=PREV HREF="s_tagbod.htm">
|
|
<LINK REL=UP HREF="c_data_a.htm">
|
|
<LINK REL=NEXT HREF="s_unwind.htm">
|
|
</HEAD>
|
|
<BODY>
|
|
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="s_tagbod.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_data_a.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="s_unwind.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<A NAME="throw"><I>Special Operator</I> <B>THROW</B></A> <P>
|
|
<P><B>Syntax:</B><P>
|
|
<P>
|
|
|
|
<B>throw</B> <I>tag result-form</I> =>| <P>
|
|
<P>
|
|
<P><B>Arguments and Values:</B><P>
|
|
<P>
|
|
<I>tag</I>---a <A REL=DEFINITION HREF="26_glo_c.htm#catch_tag"><I>catch tag</I></A>; evaluated. <P>
|
|
<I>result-form</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>; evaluated as described below. <P>
|
|
<P><B>Description:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="#throw"><B>throw</B></A> causes a non-local control transfer to a <A REL=DEFINITION HREF="s_catch.htm#catch"><B>catch</B></A> whose tag is <A REL=DEFINITION HREF="f_eq.htm#eq"><B>eq</B></A> to <I>tag</I>. <P>
|
|
<I>Tag</I> is evaluated first to produce an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> called the throw tag; then <I>result-form</I> is evaluated, and its results are saved. If the <I>result-form</I> produces multiple values, then all the values are saved. The most recent outstanding <A REL=DEFINITION HREF="s_catch.htm#catch"><B>catch</B></A> whose <I>tag</I> is <A REL=DEFINITION HREF="f_eq.htm#eq"><B>eq</B></A> to the throw tag is exited; the saved results are returned as the value or values of <A REL=DEFINITION HREF="s_catch.htm#catch"><B>catch</B></A>. <P>
|
|
The transfer of control initiated by <A REL=DEFINITION HREF="#throw"><B>throw</B></A> is performed as described in <A REL=CHILD HREF="05_b.htm">Section 5.2 (Transfer of Control to an Exit Point)</A>. <P>
|
|
<P><B>Examples:</B><P>
|
|
<P>
|
|
<PRE>
|
|
(catch 'result
|
|
(setq i 0 j 0)
|
|
(loop (incf j 3) (incf i)
|
|
(if (= i 3) (throw 'result (values i j))))) => 3, 9
|
|
|
|
</PRE>
|
|
</TT> <P>
|
|
|
|
<PRE>
|
|
(catch nil
|
|
(unwind-protect (throw nil 1)
|
|
(throw nil 2))) => 2
|
|
</PRE>
|
|
</TT> <P>
|
|
The consequences of the following are undefined because the <A REL=DEFINITION HREF="s_catch.htm#catch"><B>catch</B></A> of <TT>b</TT> is passed over by the first <A REL=DEFINITION HREF="#throw"><B>throw</B></A>, hence portable programs must assume that its <A REL=DEFINITION HREF="26_glo_d.htm#dynamic_extent"><I>dynamic extent</I></A> is terminated. The <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A> of the <A REL=DEFINITION HREF="26_glo_c.htm#catch_tag"><I>catch tag</I></A> is not yet <I>disestablished</I> and therefore it is the target of the second <A REL=DEFINITION HREF="#throw"><B>throw</B></A>. <P>
|
|
<PRE>
|
|
(catch 'a
|
|
(catch 'b
|
|
(unwind-protect (throw 'a 1)
|
|
(throw 'b 2))))
|
|
</PRE>
|
|
</TT> <P>
|
|
The following prints ``<TT>The inner catch returns :SECOND-THROW</TT>'' and then returns <TT>:outer-catch</TT>. <P>
|
|
<PRE>
|
|
(catch 'foo
|
|
(format t "The inner catch returns ~s.~%"
|
|
(catch 'foo
|
|
(unwind-protect (throw 'foo :first-throw)
|
|
(throw 'foo :second-throw))))
|
|
:outer-catch)
|
|
>> The inner catch returns :SECOND-THROW
|
|
=> :OUTER-CATCH
|
|
</PRE>
|
|
</TT> <P>
|
|
<P>
|
|
<P><B>Affected By:</B> None.
|
|
<P>
|
|
<P><B>Exceptional Situations:</B><P>
|
|
<P>
|
|
If there is no outstanding <A REL=DEFINITION HREF="26_glo_c.htm#catch_tag"><I>catch tag</I></A> that matches the throw tag, no unwinding of the stack is performed, and an error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_contro.htm#control-error"><B>control-error</B></A> is signaled. When the error is signaled, the <A REL=DEFINITION HREF="26_glo_d.htm#dynamic_environment"><I>dynamic environment</I></A> is that which was in force at the point of the <A REL=DEFINITION HREF="#throw"><B>throw</B></A>. <P>
|
|
<P><B>See Also:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="s_block.htm#block"><B>block</B></A>, <A REL=DEFINITION HREF="s_catch.htm#catch"><B>catch</B></A>, <A REL=DEFINITION HREF="s_ret_fr.htm#return-from"><B>return-from</B></A>, <A REL=DEFINITION HREF="s_unwind.htm#unwind-protect"><B>unwind-protect</B></A>, <A REL=CHILD HREF="03_a.htm">Section 3.1 (Evaluation)</A> <P>
|
|
<P><B>Notes:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="s_catch.htm#catch"><B>catch</B></A> and <A REL=DEFINITION HREF="#throw"><B>throw</B></A> are normally used when the <A REL=DEFINITION HREF="26_glo_e.htm#exit_point"><I>exit point</I></A> must have <A REL=DEFINITION HREF="26_glo_d.htm#dynamic_scope"><I>dynamic scope</I></A> (e.g., the <A REL=DEFINITION HREF="#throw"><B>throw</B></A> is not lexically enclosed by the <A REL=DEFINITION HREF="s_catch.htm#catch"><B>catch</B></A>), while <A REL=DEFINITION HREF="s_block.htm#block"><B>block</B></A> and <A REL=DEFINITION HREF="m_return.htm#return"><B>return</B></A> are used when <A REL=DEFINITION HREF="26_glo_l.htm#lexical_scope"><I>lexical scope</I></A> is sufficient. <P>
|
|
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issue</A>, <I>not part of the specification</I>, applies to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss152.htm">EXIT-EXTENT:MINIMAL</A><P></UL><HR>
|
|
|
|
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
|
|
|
|
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
|
|
</BODY>
|
|
</HTML>
|