Skip to content

catch

Catch errors from script evaluation.

Syntax

tcl
catch script ?resultVar? ?optionsVar?

Parameters

  • script: The script to evaluate
  • resultVar: Variable to store the result or error message (optional)
  • optionsVar: Variable to store options dictionary (optional)

Return Codes

CodeMeaning
0TCL_OK - Normal completion
1TCL_ERROR - Error occurred
2TCL_RETURN - return command
3TCL_BREAK - break command
4TCL_CONTINUE - continue command

Options Dictionary

When using the optionsVar parameter, catch populates it with a dictionary containing detailed information about the script execution:

For Successful Execution

KeyDescription
-codeThe return code (0 for OK)
-levelReturn level (0 for normal)

For Errors

KeyDescription
-codeThe return code (1 for ERROR)
-levelReturn level (0 for normal)
-errorinfoHuman-readable stack trace
-errorcodeMachine-readable error code (defaults to NONE)
-errorstackCall stack with argument values (INNER/CALL entries)
-errorlineLine number where error occurred

Global Variables

On error, catch automatically sets these global variables:

  • ::errorInfo - Same content as -errorinfo option
  • ::errorCode - Same content as -errorcode option (defaults to NONE)

Examples

Output
Output
Output

See Also