TCL Errors and Exceptions
TCL Errors and Exceptions
Overview
There are many things that can result in errors in Tcl commands.
If the command is a part of a larger script then the script is also aborted.
If the error occurs while executing a Tcl procedure, then the procedure is
set sum 0
foreach el $list {
For the above example the Tcl script will be aborted with the following error
message “can’t read "element": no such variable”
Tcl provides other piece of information after errors, in the global variable
errorCode.
For the above example the errorInfo will have the following
can’t read "element": no such variable
while executing
"expr $sum+$element"
invoked from within
"set sum [expr $sum+$element]..."
("foreach" body line 2)
invoked from within
"foreach el $list {
set sum [expr $sum+$element]
}"
4 VIT - SENSE 13-02-2018
Generating Errors in TCL
Error handling in Tcl is provided with the help of error and catch commands.
Syntax
Example :
}
5 VIT - SENSE 13-02-2018
Trapping errors - Catch command
Errors generally cause all active Tcl commands to be aborted.
Syntax:
set msg
Apart from errors; break, continue and return commands cause work in
progress to be aborted.
All exceptions cause active scripts to be aborted in the same way, except for two
differences.
First, the errorInfo and errorCode variables are only set during error exceptions.
Second, the exceptions other than errors are almost always caught by an
enclosing command, whereas errors usually unwind all the work in progress.
In the case of return, the string is the return value of the procedure or script.
The catch command actually catches all exceptions, not just errors.
The return value from catch indicates what kind of exception occurred and the
set string
all done
if {$b == 0} {
error "Error generated – denominator value is zero" "Info String for error" 401
} else {
ErrorCode: 401
ErrorInfo:
"Div 10 0“
Result = 5