12.5.4         catch

Description:   prevent errors from aborting entire script or command.

Syntax:         catch {script expression}

                        catch {tag}

                        catch {variable}

Arguments:    expression, tag or variable  

Returns:        error message



See Also:       error, 12.10.8 Trap and display errors (Debug a Script).



Examples:      # Example 1
catch {

if {[GETVAL FAN_START101] >= 0.5} then {

SETVAL {rotatefan101=%ROTATEPLUS 12} #this works

} }


# Example 2 - debug a script with catch and Text Tags

# pass error to text tag named text 70 and ERRORL2

# note that maximum text tag length is 70 characters

catch {SCREXEC rotate.scr} err

SETVAL text70==$err
SETVAL ERRORL2=[string range $err 71 140]


# Example 3 - debug a script using BWSPOOL

# BWSPOOL.exe must be running

# it is found in drive\webaccess\node and :\drive\webaccess\client

catch {SCREXEC rotate.scr} err

# BWSPOOL $err
BWSPOOL [concat rotate.scr $err {- -}]




The catch command may be used to prevent errors from aborting command interpretation. Catch calls the Tcl interpreter recursively to execute script, and always returns a TCL_OK code, regardless of any errors that might occur while executing script. The return value from catch is a decimal string giving the code returned by the Tcl interpreter after executing script. This will be 0 (TCL_OK) if there were no errors in a script; otherwise, it will have a non-zero value corresponding to one of the exceptional return codes (see Tcl command returns for the definitions of code values). If the varName argument is given, then it gives the name of a variable; catch will set the variable to the string returned from script (either a result or an error message). Note that catch catches all exceptions, including those generated by break and continue as well as errors.

The BWSPOOL Action Command can be used to send text messages from a script to a message spooler window (BWSPOOL.exe).    A text message can be inserted into a script and displayed in the message spool window when the BWSPOOL command is reached.

Better use is to use BWSPOOL with "catch command" to trap errors. Use a second script to call the script to be debugged as in the example 3 above. See section BWSPOOL for a complete debugger example.

Example error messages returned by catch include:

invalid command name "SETVALUE"

divide by zero

binding tag invalid

can't read "ynot": no such variable