51 lines
2.4 KiB
HTML
51 lines
2.4 KiB
HTML
|
<HTML><HEAD><TITLE>Tcl Built-In Commands - catch manual page</TITLE></HEAD><BODY>
|
||
|
<H3><A NAME="M2">NAME</A></H3>
|
||
|
catch - Evaluate script and trap exceptional returns
|
||
|
<H3><A NAME="M3">SYNOPSIS</A></H3>
|
||
|
<B>catch</B><I> script </I>?<I>varName</I>?<BR>
|
||
|
<H3><A NAME="M4">DESCRIPTION</A></H3>
|
||
|
The <B>catch</B> command may be used to prevent errors from aborting command
|
||
|
interpretation. <B>Catch</B> calls the Tcl interpreter recursively to
|
||
|
execute <I>script</I>, and always returns without raising an error,
|
||
|
regardless of any errors that might occur while executing <I>script</I>.
|
||
|
<P>
|
||
|
If <I>script</I> raises an error, <B>catch</B> will return a non-zero integer
|
||
|
value corresponding to one of the exceptional return codes (see tcl.h
|
||
|
for the definitions of code values). If the <I>varName</I> argument is
|
||
|
given, then the variable it names is set to the error message from
|
||
|
interpreting <I>script</I>.
|
||
|
<P>
|
||
|
If <I>script</I> does not raise an error, <B>catch</B> will return 0
|
||
|
(TCL_OK) and set the variable to the value returned from <I>script</I>.
|
||
|
<P>
|
||
|
Note that <B>catch</B> catches all exceptions, including those
|
||
|
generated by <B><A HREF="../TkCmd/break.htm">break</A></B> and <B><A HREF="../TkCmd/continue.htm">continue</A></B> as well as errors. The
|
||
|
only errors that are not caught are syntax errors found when the
|
||
|
script is compiled. This is because the catch command only catches
|
||
|
errors during runtime. When the catch statement is compiled, the
|
||
|
script is compiled as well and any syntax errors will generate a Tcl
|
||
|
error.
|
||
|
|
||
|
<H3><A NAME="M5">EXAMPLES</A></H3>
|
||
|
The <B>catch</B> command may be used in an <B><A HREF="../TkCmd/if.htm">if</A></B> to branch based on
|
||
|
the success of a script.
|
||
|
|
||
|
<PRE>if { [catch {open $someFile w} fid] } {
|
||
|
puts stderr "Could not open $someFile for writing\n$fid"
|
||
|
exit 1
|
||
|
}</PRE>
|
||
|
The <B>catch</B> command will not catch compiled syntax errors. The
|
||
|
first time proc <B>foo</B> is called, the body will be compiled and a
|
||
|
Tcl error will be generated.
|
||
|
|
||
|
<PRE>proc foo {} {
|
||
|
catch {expr {1 +- }}
|
||
|
}</PRE>
|
||
|
<H3><A NAME="M6">KEYWORDS</A></H3>
|
||
|
<A href="../Keywords/C.htm#catch">catch</A>, <A href="../Keywords/E.htm#error">error</A>
|
||
|
<HR><PRE>
|
||
|
<A HREF="../copyright.htm">Copyright</A> © 1993-1994 The Regents of the University of California.
|
||
|
<A HREF="../copyright.htm">Copyright</A> © 1994-1996 Sun Microsystems, Inc.
|
||
|
<A HREF="../copyright.htm">Copyright</A> © 1995-1997 Roger E. Critchlow Jr.</PRE>
|
||
|
</BODY></HTML>
|