1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/m_handle.htm
2024-04-01 10:24:07 +02:00

88 lines
6.7 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-BIND</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="v_break_.htm">
<LINK REL=UP HREF="c_condit.htm">
<LINK REL=NEXT HREF="m_hand_1.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="v_break_.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_hand_1.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<A NAME="handler-bind"><I>Macro</I> <B>HANDLER-BIND</B></A> <P>
<P><B>Syntax:</B><P>
<P>
<B>handler-bind</B> <I>({<I>binding</I>}<B>*</B>) <I>form</I><B>*</B></I> =&gt; <I><I>result</I><B>*</B></I><P>
<P>
<PRE>
binding::= (type handler)
</PRE>
<P>
<P><B>Arguments and Values:</B><P>
<P>
<I>type</I>---a <A REL=DEFINITION HREF="26_glo_t.htm#type_specifier"><I>type specifier</I></A>. <P>
<I>handler</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>; evaluated to produce a <I>handler-function</I>. <P>
<I>handler-function</I>---a <A REL=DEFINITION HREF="26_glo_d.htm#designator"><I>designator</I></A> for a <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> of one <A REL=DEFINITION HREF="26_glo_a.htm#argument"><I>argument</I></A>. <P>
<I>forms</I>---an <A REL=DEFINITION HREF="26_glo_i.htm#implicit_progn"><I>implicit progn</I></A>. <P>
<I>results</I>---the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> returned by the <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>forms</I></A>. <P>
<P><B>Description:</B><P>
<P>
Executes <I>forms</I> in a <A REL=DEFINITION HREF="26_glo_d.htm#dynamic_environment"><I>dynamic environment</I></A> where the indicated <I>handler</I> <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A> are in effect. <P>
Each <I>handler</I> should evaluate to a <I>handler-function</I>, which is used to handle <A REL=DEFINITION HREF="26_glo_c.htm#condition"><I>conditions</I></A> of the given <I>type</I> during execution of the <I>forms</I>. This <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> should take a single argument, the <A REL=DEFINITION HREF="26_glo_c.htm#condition"><I>condition</I></A> being signaled. <P>
If more than one <I>handler</I> <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A> is supplied, the <I>handler</I> <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A> are searched sequentially from top to bottom in search of a match (by visual analogy with <A REL=DEFINITION HREF="m_tpcase.htm#typecase"><B>typecase</B></A>). If an appropriate <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> is found, the associated handler is run in a <A REL=DEFINITION HREF="26_glo_d.htm#dynamic_environment"><I>dynamic environment</I></A> where none of these <I>handler</I> bindings are visible (to avoid recursive errors). If the <A REL=DEFINITION HREF="26_glo_h.htm#handler"><I>handler</I></A> <A REL=DEFINITION HREF="26_glo_d.htm#decline"><I>declines</I></A>, the search continues for another <A REL=DEFINITION HREF="26_glo_h.htm#handler"><I>handler</I></A>. <P>
If no appropriate <A REL=DEFINITION HREF="26_glo_h.htm#handler"><I>handler</I></A> is found, other <A REL=DEFINITION HREF="26_glo_h.htm#handler"><I>handlers</I></A> are sought from dynamically enclosing contours. If no <A REL=DEFINITION HREF="26_glo_h.htm#handler"><I>handler</I></A> is found outside, then <A REL=DEFINITION HREF="f_signal.htm#signal"><B>signal</B></A> returns or <A REL=DEFINITION HREF="f_error.htm#error"><B>error</B></A> enters the debugger. <P>
<P><B>Examples:</B><P>
<P>
In the following code, if an unbound variable error is signaled in the body (and not handled by an intervening handler), the first function is called. <P>
<PRE>
(handler-bind ((unbound-variable #'(lambda ...))
(error #'(lambda ...)))
...)
</PRE>
</TT> <P>
If any other kind of error is signaled, the second function is called. In either case, neither handler is active while executing the code in the associated function. <P>
<PRE>
(defun trap-error-handler (condition)
(format *error-output* &quot;~&amp;~A~&amp;&quot; condition)
(throw 'trap-errors nil))
(defmacro trap-errors (&amp;rest forms)
`(catch 'trap-errors
(handler-bind ((error #'trap-error-handler))
,@forms)))
(list (trap-errors (signal &quot;Foo.&quot;) 1)
(trap-errors (error &quot;Bar.&quot;) 2)
(+ 1 2))
&gt;&gt; Bar.
=&gt; (1 NIL 3)
</PRE>
</TT> <P>
Note that ``Foo.'' is not printed because the condition made by <A REL=DEFINITION HREF="f_signal.htm#signal"><B>signal</B></A> is a <A REL=DEFINITION HREF="26_glo_s.htm#simple_condition"><I>simple condition</I></A>, which is not of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_error.htm#error"><B>error</B></A>, so it doesn't trigger the handler for <A REL=DEFINITION HREF="e_error.htm#error"><B>error</B></A> set up by <TT>trap-errors</TT>. <P>
<P><B>Side Effects:</B> None.
<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_hand_1.htm#handler-case"><B>handler-case</B></A> <P>
<P><B>Notes:</B> None.
<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>