Chapter 3. Error Handling

In this chapter, we will cover:

  • Using the catch command
  • Using the eval command
  • Using the error command
  • Error handling procedure

Introduction

As discussed in the introduction to this book, I eventually learned the true power of the Tcl shell and how it can be used to locate and diagnose issues within the code. By using the puts statement I was able to track changes in variables and isolate sections of the code that were presenting issues.

In this chapter we will investigate the commands built into the Tcl shell that allow for more elegant error handling and isolation of sections of code to ensure that they perform correctly before proceeding with a procedure.

Error handling within Tcl allows the developer the freedom to determine how to react to an error. In the following sections we will explore Tcl error handling by implementing these control constructs to illustrate how you can use error handling to proceed with the command without raising an error or returning a Tcl error code. Based on which error handling command you decide to use, you can react accordingly and present the end user with the desired result.

The error handling constructs are as follows:

Control construct

Explanation

catch

catch script result optionalVarName.

The catch command will evaluate a script and trap all exceptional returns.

It the optionalVarName is provided it is set to the result of the evaluation.

eval

eval argument.

The eval command accepts one or more arguments that comprise a Tcl script containing one or more commands.

Returns the result of the evaluation.

error

error message information code.

The error command generates an error and is used to create the messages to be logged or returned to the end user.

By using error handling control constructs, we can not only determine in advance if an error might occur, but also present the end user with information or instructions on how to proceed.

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

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