0% found this document useful (0 votes)
156 views15 pages

TCL Errors and Exceptions

Uploaded by

suburaaj
The document discusses errors and exceptions in Tcl scripts. It explains that errors can occur for reasons like commands not existing, wrong number of arguments, or issues executing commands. Errors cause scripts to abort. It provides an example of a script error due to a variable not existing. The errorInfo and errorCode variables store error information. The catch command allows trapping errors to continue script execution. Other exceptions besides errors like break and return also cause script abortion.

Copyright:

© All Rights Reserved

Available Formats

Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
156 views15 pages

TCL Errors and Exceptions

Uploaded by

suburaaj
The document discusses errors and exceptions in Tcl scripts. It explains that errors can occur for reasons like commands not existing, wrong number of arguments, or issues executing commands. Errors cause scripts to abort. It provides an example of a script error due to a variable not existing. The errorInfo and errorCode variables store error information. The catch command allows trapping errors to continue script execution. Other exceptions besides errors like break and return also cause script abortion.

Copyright:

© All Rights Reserved

Available Formats

Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

TCL – Errors and Exceptions

Overview
 There are many things that can result in errors in Tcl commands.

 Errors can occur because

 A command doesn’t exist

 It doesn’t receive the right number of arguments

 The arguments have the wrong form or

 Some other problem occurs in executing the command

 If a Tcl error occurs the current command is aborted.

 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

aborted, and so on until all the active procedures have aborted.

2 VIT - SENSE 13-02-2018


Example of Error Script

set list {44 16 123 98 57}

set sum 0

foreach el $list {

set sum [expr $sum+$element]

For the above example the Tcl script will be aborted with the following error
message “can’t read "element": no such variable”

3 VIT - SENSE 13-02-2018


ErrorInfo and errorCode variables
 To locate the error, Tcl will create a stack trace and stores the error

information in the global variable called errorInfo.

 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.

 We can create our own custom error messages.

Syntax

error message? Info? Code?

 In the above error command syntax, it has 3 arguments.

 message is the error message

 info is set in the global variable errorInfo and

 code is set in the global variable errorCode

Example :

if {($x < 0} || ($x > 100)} {

error "x is out of range ($x)"

}
5 VIT - SENSE 13-02-2018
Trapping errors - Catch command
 Errors generally cause all active Tcl commands to be aborted.

 There may be some situations where we need to continue executing a script

even after an error has occurred.

 Catch command is used for this purpose.

Syntax:

catch {script} resultVarName

 The catch command has 2 arguments

 script is the code to be executed

 resultVarName is variable that holds the error or the result

 The catch command returns 0 if there is no error and 1 if there is an error.

6 VIT - SENSE 13-02-2018


Trapping errors - Catch command
Example :

set msg

catch {unset x} msg

msg can’t unset "x": no such variable

7 VIT - SENSE 13-02-2018


Exceptions
 Errors are just one example of a set of events called exceptions.

 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.

8 VIT - SENSE 13-02-2018


Exceptions
 All exceptions are accompanied by a string value.

 In the case of an error, the string is the error message.

 In the case of return, the string is the return value of the procedure or script.

 In the case of break and continue the string is always empty.

 The catch command actually catches all exceptions, not just errors.

 The return value from catch indicates what kind of exception occurred and the

variable specified in catch’s second argument is set to hold the string


associated with the exception.

9 VIT - SENSE 13-02-2018


Examples
Example :

catch {return "all done"} string

set string

all done

10 VIT - SENSE 13-02-2018


Tcl Exceptions
Return value from Description
catch

0 Normal return. String gives return value.

1 Error. String gives message describing the problem.

2 The return command was invoked. String gives return


value for procedure or source command.

3 The break command was invoked. String is empty.

4 The continue command was invoked. String is empty.

11 VIT - SENSE 13-02-2018


Example
proc Div {a b} {

if {$b == 0} {

error "Error generated – denominator value is zero" "Info String for error" 401

} else {

return [expr $a/$b]

if {[catch {puts "Result = [Div 10 0]"} errmsg]} {

puts "ErrorMsg: $errmsg“; puts "ErrorCode: $errorCode“; puts "ErrorInfo:\n$errorInfo\n"

if {[catch {puts "Result = [Div 10 2]"} errmsg]} {

puts "ErrorMsg: $errmsg“; puts "ErrorCode: $errorCode“; puts "ErrorInfo:\n$errorInfo\n"

} VIT - SENSE 13-02-2018


12
Example Contd.,
When above code is executed, it produces following result.

ErrorMsg: Error generated – denominator value is zero

ErrorCode: 401

ErrorInfo:

Info String for error

(procedure "Div" line 1)

invoked from within

"Div 10 0“

Result = 5

13 VIT - SENSE 13-02-2018


Exercise
Set up an associative array that contains a few city names and their
corresponding zip codes. Make at-least 5 elements in the array. Display the
names of the cities and ask the user to enter one. Display the corresponding
zip code if a city entered by the user is in the array. If the city is not in the
array then generate an error message and print the error message.

14 VIT - SENSE 13-02-2018


Thank You…

You might also like