Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/catch.n @ 43

Last change on this file since 43 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 4.5 KB
Line 
1'\"
2'\" Copyright (c) 1993-1994 The Regents of the University of California.
3'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4'\" Contributions from Don Porter, NIST, 2003.  (not subject to US copyright)
5'\"
6'\" See the file "license.terms" for information on usage and redistribution
7'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
8'\"
9'\" RCS: @(#) $Id: catch.n,v 1.18 2007/12/13 15:22:32 dgp Exp $
10'\"
11.so man.macros
12.TH catch n "8.5" Tcl "Tcl Built-In Commands"
13.BS
14'\" Note:  do not modify the .SH NAME line immediately below!
15.SH NAME
16catch \- Evaluate script and trap exceptional returns
17.SH SYNOPSIS
18\fBcatch\fI script \fR?\fIresultVarName\fR? ?\fIoptionsVarName\fR?
19.BE
20
21.SH DESCRIPTION
22.PP
23The \fBcatch\fR command may be used to prevent errors from aborting command
24interpretation.  The \fBcatch\fR command calls the Tcl interpreter recursively to
25execute \fIscript\fR, and always returns without raising an error,
26regardless of any errors that might occur while executing \fIscript\fR.
27.PP
28If \fIscript\fR raises an error, \fBcatch\fR will return a non-zero integer
29value corresponding to the exceptional return code returned by evaluation
30of \fIscript\fR.  Tcl defines the normal return code from script
31evaluation to be zero (0), or \fBTCL_OK\fR.  Tcl also defines four exceptional
32return codes: 1 (\fBTCL_ERROR\fR), 2 (\fBTCL_RETURN\fR), 3 (\fBTCL_BREAK\fR),
33and 4 (\fBTCL_CONTINUE\fR).  Errors during evaluation of a script are indicated
34by a return code of \fBTCL_ERROR\fR.  The other exceptional return codes are
35returned by the \fBreturn\fR, \fBbreak\fR, and \fBcontinue\fR commands
36and in other special situations as documented.  Tcl packages can define
37new commands that return other integer values as return codes as well,
38and scripts that make use of the \fBreturn -code\fR command can also
39have return codes other than the five defined by Tcl.
40.PP
41If the \fIresultVarName\fR argument is given, then the variable it names is
42set to the result of the script evaluation.  When the return code from
43the script is 1 (\fBTCL_ERROR\fR), the value stored in \fIresultVarName\fR is an error
44message.  When the return code from the script is 0 (\fBTCL_OK\fR), the value
45stored in \fIresultVarName\fR is the value returned from \fIscript\fR.
46.PP
47.VS 8.5
48If the \fIoptionsVarName\fR argument is given, then the variable it
49names is set to a dictionary of return options returned by evaluation
50of \fIscript\fR.  Tcl specifies two entries that are always
51defined in the dictionary: \fB\-code\fR and \fB\-level\fR.  When
52the return code from evaluation of \fIscript\fR is not \fBTCL_RETURN\fR,
53the value of the \fB\-level\fR entry will be 0, and the value
54of the \fB\-code\fR entry will be the same as the return code.
55Only when the return code is \fBTCL_RETURN\fR will the values of
56the \fB\-level\fR and \fB\-code\fR entries be something else, as
57further described in the documentation for the \fBreturn\fR command.
58.PP
59When the return code from evaluation of \fIscript\fR is \fBTCL_ERROR\fR,
60three additional entries are defined in the dictionary of return options
61stored in \fIoptionsVarName\fR: \fB\-errorinfo\fR, \fB\-errorcode\fR,
62and \fB\-errorline\fR.  The value of the \fB\-errorinfo\fR entry
63is a formatted stack trace containing more information about
64the context in which the error happened.  The formatted stack
65trace is meant to be read by a person.  The value of
66the \fB\-errorcode\fR entry is additional information about the
67error stored as a list.  The \fB\-errorcode\fR value is meant to
68be further processed by programs, and may not be particularly
69readable by people.  The value of the \fB\-errorline\fR entry
70is an integer indicating which line of \fIscript\fR was being
71evaluated when the error occurred.  The values of the \fB\-errorinfo\fR
72and \fB\-errorcode\fR entries of the most recent error are also
73available as values of the global variables \fB::errorInfo\fR
74and \fB::errorCode\fR respectively.
75.PP
76Tcl packages may provide commands that set other entries in the
77dictionary of return options, and the \fBreturn\fR command may be
78used by scripts to set return options in addition to those defined
79above.
80.VE 8.5
81.SH EXAMPLES
82The \fBcatch\fR command may be used in an \fBif\fR to branch based on
83the success of a script.
84.CS
85if { [\fBcatch\fR {open $someFile w} fid] } {
86    puts stderr "Could not open $someFile for writing\en$fid"
87    exit 1
88}
89.CE
90.PP
91There are more complex examples of \fBcatch\fR usage in the
92documentation for the \fBreturn\fR command.
93
94.SH "SEE ALSO"
95break(n), continue(n), dict(n), error(n), return(n), tclvars(n)
96
97.SH KEYWORDS
98catch, error
Note: See TracBrowser for help on using the repository browser.