Debugging and Error Trapping

Project: In the course of developing a complex script, the script hangs and doesn’t finish the whole sequence of steps. The Script developer would like to find out if there is an endless loop, or a command that did not execute, or some other problem in the script causing it to work improperly.

Algorithm: Since the developer does not know exactly where the problem occurs, the DEBUG command can be used to display the commands executed. That will give a quick indication if it is an endless loop, or if the script is waiting at a particular command. Depending on the size of the project and isolation of the problem, the DEBUG output can be written to a file or displayed in a separate window, and run in step mode or continually. If the Script developer was testing a suspect command which would fail but not hold up the script, then checking the value of the ERROR( ) function and RESULT( ) function would be the best approach.

Relevant Commands and Functions:

DEBUG — Writes script commands to file or display as they are executed
SHOW — Resumes command audit initiated by DEBUG command
NOSHOW — Suspends debug mode initiated by DEBUG command
ERROR( ) — Indicates whether an error occured in execution
RESULT( ) — Returns contents of the RESULT string
EOF( ) — Indicates if end of table has been reached
WHEN ERROR — Activates when an execution error is encountered.
*SYSTEM 0x00FF — Available in some products, writes a message to a Debug window

A Brief Example

; Script commands that are suspected to have problems
$var1 = "hello"
$var2 = "goodbye"
perform DisplayStrVar($var1)
$var3 = "all done"

By specifying an output filename of "?", this example uses the Debugger in Step Mode, which means that each command is displayed before it is executed, and the developer chooses Resume to continue. The SHOW command is placed in the script where the Debugging should start, and NOSHOW can be used to turn it back off. In this example, we also make use of a special SYSTEM command that writes a message to the Script Messages section of the debug window to display the value of a variable. Note that this command only works if a Debug window is displayed at the time it is executed. An alternate way to reporting variable values is to use a utility routine like:

DIALOG "DisplayStrVar"
MESSAGE $value
which allows you to read a value, click on OK, and continue the script.

Further Development: