143 lines
9.3 KiB
HTML
143 lines
9.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: Macro HANDLER-CASE</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="m_handle.htm">
|
|
<LINK REL=UP HREF="c_condit.htm">
|
|
<LINK REL=NEXT HREF="m_ignore.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="m_handle.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_condit.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="m_ignore.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<A NAME="handler-case"><I>Macro</I> <B>HANDLER-CASE</B></A> <P>
|
|
<P>
|
|
<P><B>Syntax:</B><P>
|
|
<P>
|
|
|
|
<B>handler-case</B> <I><I>expression</I> [[{<I>error-clause</I>}<B>*</B> | <I>no-error-clause</I>]]</I> => <I><I>result</I><B>*</B></I><P>
|
|
<P>
|
|
<PRE>
|
|
clause::= error-clause | no-error-clause
|
|
</PRE>
|
|
<PRE>
|
|
error-clause::= (typespec ([var]) declaration* form*)
|
|
</PRE>
|
|
<PRE>
|
|
no-error-clause::= (:no-error lambda-list declaration* form*)
|
|
</PRE>
|
|
<P>
|
|
<P><B>Arguments and Values:</B><P>
|
|
<P>
|
|
<I>expression</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>. <P>
|
|
<I>typespec</I>---a <A REL=DEFINITION HREF="26_glo_t.htm#type_specifier"><I>type specifier</I></A>. <P>
|
|
<I>var</I>---a <A REL=DEFINITION HREF="26_glo_v.htm#variable"><I>variable</I></A> <A REL=DEFINITION HREF="26_glo_n.htm#name"><I>name</I></A>. <P>
|
|
<I>lambda-list</I>---an <A REL=DEFINITION HREF="26_glo_o.htm#ordinary_lambda_list"><I>ordinary lambda list</I></A>. <P>
|
|
<I>declaration</I>---a <A REL=DEFINITION HREF="s_declar.htm#declare"><B>declare</B></A> <A REL=DEFINITION HREF="26_glo_e.htm#expression"><I>expression</I></A>; not evaluated. <P>
|
|
<I>form</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>. <P>
|
|
<I>results</I>---In the normal situation, the values returned are those that result from the evaluation of <I>expression</I>; in the exceptional situation when control is transferred to a <I>clause</I>, the value of the last <I>form</I> in that <I>clause</I> is returned. <P>
|
|
<P><B>Description:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="#handler-case"><B>handler-case</B></A> executes <I>expression</I> in a <A REL=DEFINITION HREF="26_glo_d.htm#dynamic_environment"><I>dynamic environment</I></A> where various handlers are active. Each <I>error-clause</I> specifies how to handle a <A REL=DEFINITION HREF="26_glo_c.htm#condition"><I>condition</I></A> matching the indicated <I>typespec</I>. A <I>no-error-clause</I> allows the specification of a particular action if control returns normally. <P>
|
|
If a <A REL=DEFINITION HREF="26_glo_c.htm#condition"><I>condition</I></A> is signaled for which there is an appropriate <I>error-clause</I> during the execution of <I>expression</I> (i.e., one for which <TT>(typep <A REL=DEFINITION HREF="26_glo_c.htm#condition"></TT><I>condition</I><TT></A> '</TT><I>typespec</I><TT>)</TT> returns <A REL=DEFINITION HREF="26_glo_t.htm#true"><I>true</I></A>) and if there is no intervening handler for a <A REL=DEFINITION HREF="26_glo_c.htm#condition"><I>condition</I></A> of that <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A>, then control is transferred to the body of the relevant <I>error-clause</I>. In this case, the dynamic state is unwound appropriately (so that the handlers established around the <I>expression</I> are no longer active), and <I>var</I> is bound to the <A REL=DEFINITION HREF="26_glo_c.htm#condition"><I>condition</I></A> that had been signaled. If more than one case is provided, those cases are made accessible in parallel. That is, in <P>
|
|
<PRE>
|
|
(handler-case form
|
|
(typespec1 (var1) form1)
|
|
(typespec2 (var2) form2))
|
|
</PRE>
|
|
</TT> <P>
|
|
if the first <I>clause</I> (containing <I>form1</I>) has been selected, the handler for the second is no longer visible (or vice versa). <P>
|
|
The <I>clauses</I> are searched sequentially from top to bottom. If there is <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> overlap between <I>typespecs</I>, the earlier of the <I>clauses</I> is selected. <P>
|
|
If <I>var</I> is not needed, it can be omitted. That is, a <I>clause</I> such as: <P>
|
|
<PRE>
|
|
(typespec (var) (declare (ignore var)) form)
|
|
</PRE>
|
|
</TT> <P>
|
|
can be written <TT>(</TT><I>typespec</I><TT> () </TT><I>form</I><TT>)</TT>. <P>
|
|
If there are no <I>forms</I> in a selected <I>clause</I>, the case, and therefore <A REL=DEFINITION HREF="#handler-case"><B>handler-case</B></A>, returns <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. If execution of <I>expression</I> returns normally and no <I>no-error-clause</I> exists, the values returned by <I>expression</I> are returned by <A REL=DEFINITION HREF="#handler-case"><B>handler-case</B></A>. If execution of <I>expression</I> returns normally and a <I>no-error-clause</I> does exist, the values returned are used as arguments to the function described by constructing <TT>(lambda </TT><I>lambda-list</I><TT> </TT><I>form</I><TT></TT><B>*</B><TT>)</TT> from the <I>no-error-clause</I>, and the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> of that function call are returned by <A REL=DEFINITION HREF="#handler-case"><B>handler-case</B></A>. The handlers which were established around the <I>expression</I> are no longer active at the time of this call. <P>
|
|
<P><B>Examples:</B><P>
|
|
<P>
|
|
<PRE>
|
|
(defun assess-condition (condition)
|
|
(handler-case (signal condition)
|
|
(warning () "Lots of smoke, but no fire.")
|
|
((or arithmetic-error control-error cell-error stream-error)
|
|
(condition)
|
|
(format nil "~S looks especially bad." condition))
|
|
(serious-condition (condition)
|
|
(format nil "~S looks serious." condition))
|
|
(condition () "Hardly worth mentioning.")))
|
|
=> ASSESS-CONDITION
|
|
(assess-condition (make-condition 'stream-error :stream *terminal-io*))
|
|
=> "#<STREAM-ERROR 12352256> looks especially bad."
|
|
(define-condition random-condition (condition) ()
|
|
(:report (lambda (condition stream)
|
|
(declare (ignore condition))
|
|
(princ "Yow" stream))))
|
|
=> RANDOM-CONDITION
|
|
(assess-condition (make-condition 'random-condition))
|
|
=> "Hardly worth mentioning."
|
|
</PRE>
|
|
</TT> <P>
|
|
<P><B>Affected By:</B> None.
|
|
<P>
|
|
<P><B>Exceptional Situations:</B> None.
|
|
<P>
|
|
<P><B>See Also:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="m_handle.htm#handler-bind"><B>handler-bind</B></A>, <A REL=DEFINITION HREF="m_ignore.htm#ignore-errors"><B>ignore-errors</B></A>, <A REL=CHILD HREF="09_a.htm">Section 9.1 (Condition System Concepts)</A> <P>
|
|
<P><B>Notes:</B><P>
|
|
<P>
|
|
<PRE>
|
|
(handler-case form
|
|
(type1 (var1) . body1)
|
|
(type2 (var2) . body2) ...)
|
|
</PRE>
|
|
</TT> is approximately equivalent to: <P>
|
|
<PRE>
|
|
(block #1=#:g0001
|
|
(let ((#2=#:g0002 nil))
|
|
(tagbody
|
|
(handler-bind ((type1 #'(lambda (temp)
|
|
(setq #1# temp)
|
|
(go #3=#:g0003)))
|
|
(type2 #'(lambda (temp)
|
|
(setq #2# temp)
|
|
(go #4=#:g0004))) ...)
|
|
(return-from #1# form))
|
|
#3# (return-from #1# (let ((var1 #2#)) . body1))
|
|
#4# (return-from #1# (let ((var2 #2#)) . body2)) ...)))
|
|
</PRE>
|
|
</TT> <P>
|
|
<PRE>
|
|
(handler-case form
|
|
(type1 (var1) . body1)
|
|
...
|
|
(:no-error (varN-1 varN-2 ...) . bodyN))
|
|
</PRE>
|
|
</TT> is approximately equivalent to: <P>
|
|
<PRE>
|
|
|
|
(block #1=#:error-return
|
|
(multiple-value-call #'(lambda (varN-1 varN-2 ...) . bodyN)
|
|
(block #2=#:normal-return
|
|
(return-from #1#
|
|
(handler-case (return-from #2# form)
|
|
(type1 (var1) . body1) ...)))))
|
|
</PRE>
|
|
</TT> <P>
|
|
<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/iss097.htm">DECLS-AND-DOC</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>
|