Skip to content
Tcl Source Code Documentation
Login

Documentation


CommandName: catch ManualSection: n Version: 8.5 TclPart: Tcl TclDescription: Tcl Built-In Commands Links:

Name

catch - Evaluate script and trap exceptional returns

Synopsis

::: {.synopsis} ::: [catch]{.cmd} [script]{.arg} [resultVarName]{.optarg} [optionsVarName]{.optarg} :::

Description

The fBcatchfR command may be used to prevent errors from aborting command interpretation. The fBcatchfR command calls the Tcl interpreter recursively to execute fIscriptfR, and always returns without raising an error, regardless of any errors that might occur while executing fIscriptfR.

If fIscriptfR raises an error, fBcatchfR will return a non-zero integer value corresponding to the exceptional return code returned by evaluation of fIscriptfR. Tcl defines the normal return code from script evaluation to be zero (0), or fBTCL_OKfR. Tcl also defines four exceptional return codes: 1 (fBTCL_ERRORfR), 2 (fBTCL_RETURNfR), 3 (fBTCL_BREAKfR), and 4 (fBTCL_CONTINUEfR). Errors during evaluation of a script are indicated by a return code of fBTCL_ERRORfR. The other exceptional return codes are returned by the fBreturnfR, fBbreakfR, and fBcontinuefR commands and in other special situations as documented. New commands defined by Tcl packages as well as scripts that make use of the fBreturn -codefR command can return other integer values as the return code. These must however lie outside the range reserved for Tcl as documented for the fBreturnfR command.

If the fIresultVarNamefR argument is given, then the variable it names is set to the result of the script evaluation. When the return code from the script is 1 (fBTCL_ERRORfR), the value stored in fIresultVarNamefR is an error message. When the return code from the script is 0 (fBTCL_OKfR), the value stored in fIresultVarNamefR is the value returned from fIscriptfR.

If the fIoptionsVarNamefR argument is given, then the variable it names is set to a dictionary of return options returned by evaluation of fIscriptfR. Tcl specifies two entries that are always defined in the dictionary: fB-codefR and fB-levelfR. When the return code from evaluation of fIscriptfR is not fBTCL_RETURNfR, the value of the fB-levelfR entry will be 0, and the value of the fB-codefR entry will be the same as the return code. Only when the return code is fBTCL_RETURNfR will the values of the fB-levelfR and fB-codefR entries be something else, as further described in the documentation for the fBreturnfR command.

When the return code from evaluation of fIscriptfR is fBTCL_ERRORfR, four additional entries are defined in the dictionary of return options stored in fIoptionsVarNamefR: fB-errorinfofR, fB-errorcodefR, fB-errorlinefR, and fB-errorstackfR. The value of the fB-errorinfofR entry is a formatted stack trace containing more information about the context in which the error happened. The formatted stack trace is meant to be read by a person. The value of the fB-errorcodefR entry is additional information about the error stored as a list. The fB-errorcodefR value is meant to be further processed by programs, and may not be particularly readable by people. The value of the fB-errorlinefR entry is an integer indicating which line of fIscriptfR was being evaluated when the error occurred. The value of the fB-errorstackfR entry is an even-sized list made of token-parameter pairs accumulated while unwinding the stack. The token may be "CALL", in which case the parameter is a list made of the proc name and arguments at the corresponding level; or it may be "UP", in which case the parameter is the relative level (as in fBuplevelfR) of the previous fBCALLfR. The salient differences with respect to fB-errorinfofR are that:

  1. it is a machine-readable form that is amenable to processing with [fBforeachfR {tok prm} ...],

  2. it contains the true (substituted) values passed to the functions, instead of the static text of the calling sites, and

  3. it is coarser-grained, with only one element per stack frame (like procs; no separate elements for fBforeachfR constructs for example).

The values of the fB-errorinfofR and fB-errorcodefR entries of the most recent error are also available as values of the global variables fB::errorInfofR and fB::errorCodefR respectively. The value of the fB-errorstackfR entry surfaces as fBinfo errorstackfR.

Tcl packages may provide commands that set other entries in the dictionary of return options, and the fBreturnfR command may be used by scripts to set return options in addition to those defined above.

Examples

The fBcatchfR command may be used in an fBiffR to branch based on the success of a script.

if { [catch {open $someFile w} fid] } {
    puts stderr "Could not open $someFile for writing\n$fid"
    exit 1
}

There are more complex examples of fBcatchfR usage in the documentation for the fBreturnfR command.