| 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 |
.TH catch n "8.5" Tcl "Tcl Built-In Commands" |
| 10 |
.so man.macros |
| 11 |
.BS |
| 12 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 13 |
.SH NAME |
| 14 |
catch \- Evaluate script and trap exceptional returns |
| 15 |
.SH SYNOPSIS |
| 16 |
\fBcatch\fI script \fR?\fIresultVarName\fR? ?\fIoptionsVarName\fR? |
| 17 |
.BE |
| 18 |
.SH DESCRIPTION |
| 19 |
.PP |
| 20 |
The \fBcatch\fR command may be used to prevent errors from aborting command |
| 21 |
interpretation. The \fBcatch\fR command calls the Tcl interpreter recursively |
| 22 |
to execute \fIscript\fR, and always returns without raising an error, |
| 23 |
regardless of any errors that might occur while executing \fIscript\fR. |
| 24 |
.PP |
| 25 |
If \fIscript\fR raises an error, \fBcatch\fR will return a non-zero integer |
| 26 |
value corresponding to the exceptional return code returned by evaluation |
| 27 |
of \fIscript\fR. Tcl defines the normal return code from script |
| 28 |
evaluation to be zero (0), or \fBTCL_OK\fR. Tcl also defines four exceptional |
| 29 |
return codes: 1 (\fBTCL_ERROR\fR), 2 (\fBTCL_RETURN\fR), 3 (\fBTCL_BREAK\fR), |
| 30 |
and 4 (\fBTCL_CONTINUE\fR). Errors during evaluation of a script are indicated |
| 31 |
by a return code of \fBTCL_ERROR\fR. The other exceptional return codes are |
| 32 |
returned by the \fBreturn\fR, \fBbreak\fR, and \fBcontinue\fR commands |
| 33 |
and in other special situations as documented. Tcl packages can define |
| 34 |
new commands that return other integer values as return codes as well, |
| 35 |
and scripts that make use of the \fBreturn \-code\fR command can also |
| 36 |
have return codes other than the five defined by Tcl. |
| 37 |
.PP |
| 38 |
If the \fIresultVarName\fR argument is given, then the variable it names is |
| 39 |
set to the result of the script evaluation. When the return code from the |
| 40 |
script is 1 (\fBTCL_ERROR\fR), the value stored in \fIresultVarName\fR is an |
| 41 |
error message. When the return code from the script is 0 (\fBTCL_OK\fR), the |
| 42 |
value stored in \fIresultVarName\fR is the value returned from \fIscript\fR. |
| 43 |
.PP |
| 44 |
If the \fIoptionsVarName\fR argument is given, then the variable it |
| 45 |
names is set to a dictionary of return options returned by evaluation |
| 46 |
of \fIscript\fR. Tcl specifies two entries that are always |
| 47 |
defined in the dictionary: \fB\-code\fR and \fB\-level\fR. When |
| 48 |
the return code from evaluation of \fIscript\fR is not \fBTCL_RETURN\fR, |
| 49 |
the value of the \fB\-level\fR entry will be 0, and the value |
| 50 |
of the \fB\-code\fR entry will be the same as the return code. |
| 51 |
Only when the return code is \fBTCL_RETURN\fR will the values of |
| 52 |
the \fB\-level\fR and \fB\-code\fR entries be something else, as |
| 53 |
further described in the documentation for the \fBreturn\fR command. |
| 54 |
.PP |
| 55 |
When the return code from evaluation of \fIscript\fR is |
| 56 |
\fBTCL_ERROR\fR, four additional entries are defined in the dictionary |
| 57 |
of return options stored in \fIoptionsVarName\fR: \fB\-errorinfo\fR, |
| 58 |
\fB\-errorcode\fR, \fB\-errorline\fR, and |
| 59 |
.VS 8.6 |
| 60 |
\fB\-errorstack\fR. |
| 61 |
.VE 8.6 |
| 62 |
The value of the \fB\-errorinfo\fR entry is a formatted stack trace containing |
| 63 |
more information about the context in which the error happened. The formatted |
| 64 |
stack trace is meant to be read by a person. The value of the |
| 65 |
\fB\-errorcode\fR entry is additional information about the error stored as a |
| 66 |
list. The \fB\-errorcode\fR value is meant to be further processed by |
| 67 |
programs, and may not be particularly readable by people. The value of the |
| 68 |
\fB\-errorline\fR entry is an integer indicating which line of \fIscript\fR |
| 69 |
was being evaluated when the error occurred. |
| 70 |
.VS 8.6 |
| 71 |
The value of the \fB\-errorstack\fR entry is an |
| 72 |
even-sized list made of token-parameter pairs accumulated while |
| 73 |
unwinding the stack. The token may be |
| 74 |
.QW \fBCALL\fR , |
| 75 |
in which case the parameter is a list made of the proc name and arguments at |
| 76 |
the corresponding level; or it may be |
| 77 |
.QW \fBUP\fR , |
| 78 |
in which case the parameter is |
| 79 |
the relative level (as in \fBuplevel\fR) of the previous \fBCALL\fR. The |
| 80 |
salient differences with respect to \fB\-errorinfo\fR are that: |
| 81 |
.IP [1] |
| 82 |
it is a machine-readable form that is amenable to processing with |
| 83 |
[\fBforeach\fR {tok prm} ...], |
| 84 |
.IP [2] |
| 85 |
it contains the true (substituted) values passed to the functions, instead of |
| 86 |
the static text of the calling sites, and |
| 87 |
.IP [3] |
| 88 |
it is coarser-grained, with only one element per stack frame (like procs; no |
| 89 |
separate elements for \fBforeach\fR constructs for example). |
| 90 |
.VE 8.6 |
| 91 |
.PP |
| 92 |
The values of the \fB\-errorinfo\fR and \fB\-errorcode\fR entries of |
| 93 |
the most recent error are also available as values of the global |
| 94 |
variables \fB::errorInfo\fR and \fB::errorCode\fR respectively. |
| 95 |
.VS 8.6 |
| 96 |
The value of the \fB\-errorstack\fR entry surfaces as \fBinfo errorstack\fR. |
| 97 |
.VE 8.6 |
| 98 |
.PP |
| 99 |
Tcl packages may provide commands that set other entries in the |
| 100 |
dictionary of return options, and the \fBreturn\fR command may be |
| 101 |
used by scripts to set return options in addition to those defined |
| 102 |
above. |
| 103 |
.SH EXAMPLES |
| 104 |
.PP |
| 105 |
The \fBcatch\fR command may be used in an \fBif\fR to branch based on |
| 106 |
the success of a script. |
| 107 |
.PP |
| 108 |
.CS |
| 109 |
if { [\fBcatch\fR {open $someFile w} fid] } { |
| 110 |
puts stderr "Could not open $someFile for writing\en$fid" |
| 111 |
exit 1 |
| 112 |
} |
| 113 |
.CE |
| 114 |
.PP |
| 115 |
There are more complex examples of \fBcatch\fR usage in the |
| 116 |
documentation for the \fBreturn\fR command. |
| 117 |
.SH "SEE ALSO" |
| 118 |
break(n), continue(n), dict(n), error(n), errorCode(n), errorInfo(n), info(n), |
| 119 |
return(n) |
| 120 |
.SH KEYWORDS |
| 121 |
catch, error, exception |
| 122 |
'\" Local Variables: |
| 123 |
'\" mode: nroff |
| 124 |
'\" fill-column: 78 |
| 125 |
'\" End: |