1
0
Fork 0
cl-sites/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node324.html

92 lines
4.4 KiB
HTML
Raw Normal View History

2023-10-25 11:23:21 +02:00
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>29.3.9. Comparison of Restarts and Catch/Throw</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Comparison of Restarts and Catch/Throw">
<meta name="keywords" value="clm">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<b>Common Lisp the Language, 2nd Edition</b>
<BR> <HR><A NAME=tex2html5743 HREF="node325.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html5741 HREF="node315.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html5735 HREF="node323.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html5745 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html5746 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html5744 HREF="node325.html"> Generalized Restarts</A>
<B>Up:</B> <A NAME=tex2html5742 HREF="node315.html"> Survey of Concepts</A>
<B> Previous:</B> <A NAME=tex2html5736 HREF="node323.html"> Restart Functions</A>
<HR> <P>
<H2><A NAME=SECTION003339000000000000000>29.3.9. Comparison of Restarts and Catch/Throw</A></H2>
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
One important feature that <tt>restart-case</tt> (or <tt>restart-bind</tt>) offers that
<tt>catch</tt> does not is the ability to reason about the available points to
which control might be transferred without actually attempting the
transfer. One could, for example, write
<P><pre>
(ignore-errors (throw ...))
</pre><P>
which is a sort of poor man's variation of
<P><pre>
(when (find-restart 'something)
(invoke-restart 'something))
</pre><P>
but there is no way to use <tt>ignore-errors</tt> and <tt>throw</tt> to simulate something
like
<P><pre>
(when (and (find-restart 'something)
(find-restart 'something-else))
(invoke-restart 'something))
</pre><P>
or even just
<P><pre>
(when (and (find-restart 'something)
(yes-or-no-p &quot;Do something? &quot;))
(invoke-restart 'something))
</pre><P>
because the degree of inspectability that comes with simply writing
<P><pre>
(ignore-errors (throw ...))
</pre><P>
is too primitive-getting the desired information also forces
transfer of control, perhaps at a time when it is not desirable.
<P>
Many programmers have previously evolved strategies like the following
on a case-by-case basis:
<P><pre>
(defvar *foo-tag-is-available* nil)
(defun fn-1 ()
(catch 'foo
(let ((*foo-tag-is-available* t))
... (fn-2) ...)))
(defun fn-2 ()
...
(if *foo-tag-is-available* (throw 'foo t))
...)
</pre><P>
The facility provided by <tt>restart-case</tt> and <tt>find-restart</tt> is intended to
provide a standardized protocol for this sort of information to be
communicated between programs that were developed independently so that
individual variations from program to program
do not thwart the overall modularity and debuggability of programs.
<P>
Another difference between the restart facility and the <tt>catch</tt>/<tt>throw</tt>
facility is that a <tt>catch</tt> with any given tag completely shadows any
outer pending <tt>catch</tt> that uses the same tag. Because of the presence
of <tt>compute-restarts</tt>, however, it is possible to see shadowed restarts,
which may be very useful in some situations (particularly in an
interactive debugger).
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR> <HR><A NAME=tex2html5743 HREF="node325.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html5741 HREF="node315.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html5735 HREF="node323.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html5745 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html5746 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html5744 HREF="node325.html"> Generalized Restarts</A>
<B>Up:</B> <A NAME=tex2html5742 HREF="node315.html"> Survey of Concepts</A>
<B> Previous:</B> <A NAME=tex2html5736 HREF="node323.html"> Restart Functions</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>