Using the error command

The primary usage of the error command is to programmatically raise an error. This allows you to interrupt the interpreter at the desired point and to present the user with an error message of your choice.

Getting ready

To complete the following example, we will need to create a Tcl script file in your working directory. Open the text editor of your choice and follow the given instructions.

How to do it…

In the following example, to illustrate, we will generate an error and a supporting error message in a location where it should not occur. Using the editor of your choice, create a text file named error.tcl that contains the following commands:

if {1+1==2} {
error "My Error"
}

After you have created the file, invoke the script with the following command line:


% tclsh85 error.tcl
My Error
	while executing
"error "My Error""
	invoked from within
"if {1+1==2} {
	error "My Error"
}"
	(file "error.tcl" line 1)
child process exited abnormally
%

How it works…

As you can see, in the command line output, we generated an error message (My Error) as well as the Tcl error messaging; although there was no error in the math function. While this is an unrealistic implementation, it illustrates the ability to raise an error at the desired point, with the message of your choice.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.147.77