73 lines
7.3 KiB
HTML
73 lines
7.3 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 CATCH</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_block.htm">
|
||
|
<LINK REL=UP HREF="c_data_a.htm">
|
||
|
<LINK REL=NEXT HREF="s_go.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_block.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_go.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
||
|
|
||
|
<HR>
|
||
|
|
||
|
<A NAME="catch"><I>Special Operator</I> <B>CATCH</B></A> <P>
|
||
|
<P><B>Syntax:</B><P>
|
||
|
<P>
|
||
|
|
||
|
<B>catch</B> <I><I>tag</I> <I>form</I><B>*</B></I> => <I><I>result</I><B>*</B></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>forms</I>---an <A REL=DEFINITION HREF="26_glo_i.htm#implicit_progn"><I>implicit progn</I></A>. <P>
|
||
|
<I>results</I>---if the <I>forms</I> exit normally, the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> returned by the <I>forms</I>; if a throw occurs to the <I>tag</I>, the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> that are thrown. <P>
|
||
|
<P><B>Description:</B><P>
|
||
|
<P>
|
||
|
<A REL=DEFINITION HREF="#catch"><B>catch</B></A> is used as the destination of a non-local control transfer by <A REL=DEFINITION HREF="s_throw.htm#throw"><B>throw</B></A>. <I>Tags</I> are used to find the <A REL=DEFINITION HREF="#catch"><B>catch</B></A> to which a <A REL=DEFINITION HREF="s_throw.htm#throw"><B>throw</B></A> is transferring control. <TT>(catch 'foo </TT><I>form</I><TT>)</TT> catches a <TT>(throw 'foo </TT><I>form</I><TT>)</TT> but not a <TT>(throw 'bar </TT><I>form</I><TT>)</TT>. <P>
|
||
|
The order of execution of <A REL=DEFINITION HREF="#catch"><B>catch</B></A> follows: <P>
|
||
|
<P><DL><DT>1. <I>Tag</I> is evaluated. It serves as the name of the <A REL=DEFINITION HREF="#catch"><B>catch</B></A>. <P><DD>
|
||
|
<DT>2. <I>Forms</I> are then evaluated as an implicit <A REL=DEFINITION HREF="s_progn.htm#progn"><B>progn</B></A>, and the results of the last <I>form</I> are returned unless a <A REL=DEFINITION HREF="s_throw.htm#throw"><B>throw</B></A> occurs. <P><DD>
|
||
|
<DT>3. If a <A REL=DEFINITION HREF="s_throw.htm#throw"><B>throw</B></A> occurs during the execution of one of the <I>forms</I>, control is transferred to the <A REL=DEFINITION HREF="#catch"><B>catch</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> whose <I>tag</I> is <A REL=DEFINITION HREF="f_eq.htm#eq"><B>eq</B></A> to the tag argument of the <A REL=DEFINITION HREF="s_throw.htm#throw"><B>throw</B></A> and which is the most recently established <A REL=DEFINITION HREF="#catch"><B>catch</B></A> with that <I>tag</I>. No further evaluation of <I>forms</I> occurs. <P><DD>
|
||
|
<DT>4. The <I>tag</I> <I>established</I> by <A REL=DEFINITION HREF="#catch"><B>catch</B></A> is <I>disestablished</I> just before the results are returned. <P><DD>
|
||
|
<P></DL><P>
|
||
|
If during the execution of one of the <I>forms</I>, a <A REL=DEFINITION HREF="s_throw.htm#throw"><B>throw</B></A> is executed whose tag is <A REL=DEFINITION HREF="f_eq.htm#eq"><B>eq</B></A> to the <A REL=DEFINITION HREF="#catch"><B>catch</B></A> tag, then the values specified by the <A REL=DEFINITION HREF="s_throw.htm#throw"><B>throw</B></A> are returned as the result of the dynamically most recently established <A REL=DEFINITION HREF="#catch"><B>catch</B></A> form with that tag. <P>
|
||
|
The mechanism for <A REL=DEFINITION HREF="#catch"><B>catch</B></A> and <A REL=DEFINITION HREF="s_throw.htm#throw"><B>throw</B></A> works even if <A REL=DEFINITION HREF="s_throw.htm#throw"><B>throw</B></A> is not within the lexical scope of <A REL=DEFINITION HREF="#catch"><B>catch</B></A>. <A REL=DEFINITION HREF="s_throw.htm#throw"><B>throw</B></A> must occur within the <A REL=DEFINITION HREF="26_glo_d.htm#dynamic_extent"><I>dynamic extent</I></A> of the <A REL=DEFINITION HREF="26_glo_e.htm#evaluation"><I>evaluation</I></A> of the body of a <A REL=DEFINITION HREF="#catch"><B>catch</B></A> with a corresponding <I>tag</I>. <P>
|
||
|
<P><B>Examples:</B><P>
|
||
|
|
||
|
<PRE>
|
||
|
(catch 'dummy-tag 1 2 (throw 'dummy-tag 3) 4) => 3
|
||
|
(catch 'dummy-tag 1 2 3 4) => 4
|
||
|
(defun throw-back (tag) (throw tag t)) => THROW-BACK
|
||
|
(catch 'dummy-tag (throw-back 'dummy-tag) 2) => T
|
||
|
|
||
|
;; Contrast behavior of this example with corresponding example of BLOCK.
|
||
|
(catch 'c
|
||
|
(flet ((c1 () (throw 'c 1)))
|
||
|
(catch 'c (c1) (print 'unreachable))
|
||
|
2)) => 2
|
||
|
</PRE>
|
||
|
</TT> <P>
|
||
|
<P><B>Affected By:</B> None.
|
||
|
<P><B>Exceptional Situations:</B><P>
|
||
|
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 if <A REL=DEFINITION HREF="s_throw.htm#throw"><B>throw</B></A> is done when there is no suitable <A REL=DEFINITION HREF="#catch"><B>catch</B></A> <I>tag</I>. <P><B>See Also:</B><P>
|
||
|
<P>
|
||
|
<A REL=DEFINITION HREF="s_throw.htm#throw"><B>throw</B></A>, <A REL=CHILD HREF="03_a.htm">Section 3.1 (Evaluation)</A> <P>
|
||
|
<P><B>Notes:</B><P>
|
||
|
<P>
|
||
|
It is customary for <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbols</I></A> to be used as <I>tags</I>, but any <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> is permitted. However, numbers should not be used because the comparison is done using <A REL=DEFINITION HREF="f_eq.htm#eq"><B>eq</B></A>. <P>
|
||
|
<A REL=DEFINITION HREF="#catch"><B>catch</B></A> differs from <A REL=DEFINITION HREF="s_block.htm#block"><B>block</B></A> in that <A REL=DEFINITION HREF="#catch"><B>catch</B></A> tags have dynamic <A REL=DEFINITION HREF="26_glo_s.htm#scope"><I>scope</I></A> while <A REL=DEFINITION HREF="s_block.htm#block"><B>block</B></A> names have <A REL=DEFINITION HREF="26_glo_l.htm#lexical_scope"><I>lexical scope</I></A>. <P>
|
||
|
<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>
|