catch(n)
β¨ catch(n) β the Tcl Built-In Commands glow-up β¨
okay bestie, gather round, because catch is literally the bodyguard of your Tcl scripts and you're gonna be obsessed π (this one lives in the Tcl Built-In Commands section of the manual, iykyk)
π NAME π
catch β evaluate a script and trap those exceptional returns, periodt
π¦ SYNOPSIS π¦
catch script ?resultVarName? ?optionsVarName?
π DESCRIPTION π
So here's the vibe: the catch command exists so errors do not abort command interpretation. Like, it's the friend who catches your drink before it hits the floor π«Ά
catch calls the Tcl interpreter recursively to execute script, and it always returns without raising an error β no matter what kind of chaos script throws at it. Iconic behaviour honestly.
If script raises an error, catch will return a non-zero integer value corresponding to the exceptional return code that came back from evaluating script. Tcl defines the normal return code from script evaluation as zero (0), a.k.a. TCL_OK. Tcl also defines four exceptional return codes:
1(TCL_ERROR)2(TCL_RETURN)3(TCL_BREAK)4(TCL_CONTINUE)
Errors that happen while evaluating a script are flagged with a return code of TCL_ERROR. The other exceptional return codes come back from the return, break, and continue commands, plus in other special situations as documented. And listen β new commands defined by Tcl packages, and scripts that use the return -code command, can return other integer values as the return code too. Those ones must lie outside the range reserved for Tcl, as documented for the return command. no cap π₯
πΈ the resultVarName situation πΈ
If you pass the resultVarName argument, then the variable it names gets set to the result of the script evaluation. Here's the tea:
- when the return code from the script is
1(TCL_ERROR), the value stored inresultVarNameis an error message π - when the return code from the script is
0(TCL_OK), the value stored inresultVarNameis the value returned fromscriptβ¨
π the optionsVarName situation π
If you pass the optionsVarName argument, then the variable it names gets set to a dictionary of return options returned by evaluation of script. Tcl specifies two entries that are always defined in that dictionary: -code and -level.
When the return code from evaluation of script is not TCL_RETURN, the value of the -level entry will be 0, and the value of the -code entry will be the same as the return code. Only when the return code is TCL_RETURN will the values of the -level and -code entries be something else β as further described in the documentation for the return command.
π when it's TCL_ERROR, you get four bonus entries π
When the return code from evaluation of script is TCL_ERROR, four additional entries are defined in the dictionary of return options stored in optionsVarName:
-errorinfoβ a formatted stack trace containing more information about the context in which the error happened. This formatted stack trace is meant to be read by a person. π-errorcodeβ additional information about the error, stored as a list. This one is meant to be further processed by programs, and may not be particularly readable by people. π-errorlineβ an integer indicating which line ofscriptwas being evaluated when the error occurred.-errorstackβ 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 inuplevel) of the previousCALL.
The salient differences with respect to -errorinfo are that:
- it is a machine-readable form that is amenable to processing with
[foreach {tok prm} ...], - it contains the true (substituted) values passed to the functions, instead of the static text of the calling sites, and
- it is coarser-grained, with only one element per stack frame (like procs; no separate elements for
foreachconstructs for example).
π§ where else you'll find these π
The values of the -errorinfo and -errorcode entries of the most recent error are also available as the values of the global variables ::errorInfo and ::errorCode respectively. And the value of the -errorstack entry surfaces as info errorstack. slay π
Tcl packages may provide commands that set other entries in the dictionary of return options, and the return command may be used by scripts to set return options in addition to those defined above. the flexibility? unmatched β¨
π EXAMPLES π
The catch command may be used in an if to branch based on the success of a script. look at her go:
if { [catch {open $someFile w} fid] } {
puts stderr "Could not open $someFile for writing\n$fid"
exit 1
}
There are more complex examples of catch usage in the documentation for the return command. go read those too bestie, you'll eat π«Ά
π₯ SEE ALSO π₯
β KEYWORDS β
catch, error, exception
and that's the whole page, angel π this one shipped with Tcl 8.5, so if you're on that version you're in exactly the right place. catch you later β literally ππ¦β¨
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- Copyright (c) 1993-1994 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms