OSDN Git Service

Updated to tcl 8.4.1
[pf3gnuchains/pf3gnuchains3x.git] / tcl / doc / catch.n
1 '\"
2 '\" Copyright (c) 1993-1994 The Regents of the University of California.
3 '\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4 '\"
5 '\" See the file "license.terms" for information on usage and redistribution
6 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7 '\" 
8 '\" RCS: @(#) $Id$
9 '\" 
10 .so man.macros
11 .TH catch n "8.0" Tcl "Tcl Built-In Commands"
12 .BS
13 '\" Note:  do not modify the .SH NAME line immediately below!
14 .SH NAME
15 catch \- Evaluate script and trap exceptional returns
16 .SH SYNOPSIS
17 \fBcatch\fI script \fR?\fIvarName\fR?
18 .BE
19
20 .SH DESCRIPTION
21 .PP
22 The \fBcatch\fR command may be used to prevent errors from aborting command
23 interpretation.  \fBCatch\fR calls the Tcl interpreter recursively to
24 execute \fIscript\fR, and always returns without raising an error,
25 regardless of any errors that might occur while executing \fIscript\fR.
26 .PP
27 If \fIscript\fR raises an error, \fBcatch\fR will return a non-zero integer
28 value corresponding to one of the exceptional return codes (see tcl.h
29 for the definitions of code values).  If the \fIvarName\fR argument is
30 given, then the variable it names is set to the error message from
31 interpreting \fIscript\fR.
32 .PP
33 If \fIscript\fR does not raise an error, \fBcatch\fR will return 0
34 (TCL_OK) and set the variable to the value returned from \fIscript\fR.
35 .PP
36 Note that \fBcatch\fR catches all exceptions, including those
37 generated by \fBbreak\fR and \fBcontinue\fR as well as errors.  The
38 only errors that are not caught are syntax errors found when the
39 script is compiled.  This is because the catch command only catches
40 errors during runtime.  When the catch statement is compiled, the
41 script is compiled as well and any syntax errors will generate a Tcl
42 error. 
43
44 .SH EXAMPLES
45
46 The \fBcatch\fR command may be used in an \fBif\fR to branch based on
47 the success of a script.
48
49 .CS
50 if { [catch {open $someFile w} fid] } {
51     puts stderr "Could not open $someFile for writing\\n$fid"
52     exit 1
53 }
54 .CE
55 The \fBcatch\fR command will not catch compiled syntax errors.  The
56 first time proc \fBfoo\fR is called, the body will be compiled and a
57 Tcl error will be generated. 
58
59 .CS
60 proc foo {} {
61     catch {expr {1 +- }}
62 }
63 .CE
64
65 .SH "SEE ALSO" 
66 error(n), break(n), continue(n)
67
68 .SH KEYWORDS
69 catch, error