Error Handling

During script execution, errors may occur. These are generally called runtime errors because they occur while the script is running, as opposed to syntax errors, which are detected before a script is run. When a runtime error occurs, it throws an exception. When an exception is not caught, it causes script execution to terminate and an error message to be displayed. The try...catch...end try control structure allows you to catch these exceptions so your script can handle the error condition in the manner you choose. The throw command can be used to throw your own exceptions, or to re-throw an exception that you caught.

try ... catch ... end try

What it Does

The try statement protects a block of statements from being terminated by any exceptions that may be thrown while they are running. If such an exception is thrown, control immediately transfers to the first statement following the catch statement. If no exception is thrown, the catch block is skipped, and execution continues with the next statement following end try.

Example

to testExceptions

try

doSomething

throw "Bad Problem", "Something went wrong"

doSomethingElse -- this will not get executed

catch anException

-- do processing here to handle the error

put anException -- shows "Bad Problem"

put anException.reason -- shows "Something went wrong"

end try

-- now test the single-statement try:

try to set product to 5 * amt -- throws an exception

if the exception is not empty then put the exception

end testExceptions

Tech Talk

Syntax: try

statementsToTry

{

catch {exceptionVariable}

statementsToHandleErrors

}

end [try | catch]

try {to}singleStatementToTry

Basically, any exception which is thrown in the “try” portion will cause execution to transfer directly to the “catch” part of the script. If no exception is raised, the catch portion is skipped. Exceptions may be thrown by SenseTalk itself — for example, if there is no “doSomething” handler an Unknown Command exception will be thrown — or directly by your script using the throw command as shown. If an exception is not caught in the handler where it occurs, it may be caught by another handler which called the first one. For example, if an exception is thrown in the doSomething handler and not caught there, it will be caught here in the testExceptions handler.
The exceptionVariable name on the catch line (anException in the example) is optional. If supplied, it receives an exception object whose text value is the name of the exception. The exception object’s name property also contains the name of the exception that was thrown (“Bad Problem” in the example), and its reason property typically describes the reason for the exception in more detail (“Something went wrong” in the example). The caught exception is also available in the exception global property. See the description of the exception global property on Global Properties for Object Messages and Results.

If the catch portion is omitted, exceptions thrown in the try block will interrupt the flow of the script, preventing the remainder of statements within that try block from being executed, but will otherwise be ignored, and execution will continue with the next statement after the try block.

The single statement version of the try statement provides an easy way to execute a single command without terminating the script if it throws an exception. If an exception is thrown, execution proceeds normally with the next statement. The exception is available in the exception global property, which otherwise is set to empty.

throw

What it Does

Throws an exception, which will cause script execution to terminate with an error message unless the exception is caught. A reason may be given with the throw command by supplying it as a second parameter, or a property list with name and reason properties may be used instead:

Examples

throw "BadProblem","Something is seriously messed up!"

throw (name: "Error Code 97", reason:"Invalid Phone Number")

Tech Talk

Syntax: throw {exception} exceptionName {, exceptionReason {, additionalReason ...}}

throw {exception} exceptionObject

If an exceptionObject is given, it should typically have at least a name property, and usually a reason property, as shown in the example above. In any case, the throw command will ensure that a complete exception object is created and thrown, using the information that you supply, plus some additional details.

Deprecated functions

The exceptionName(), exceptionReason() and exceptionLocation() functions return the name, reason, and location of the caught exception within a catch block. They are now obsolete, though, and their use is discouraged. Use the exception global property instead.

tryDepth function

What it Does

This function returns the level of nesting of try blocks in effect at the current point in script execution. This can be used to quickly determine whether a thrown exception will be caught at some higher level of the call stack. A return value of zero indicates that there are currently no try blocks, so any exception that is thrown will cause script execution to terminate.

Examples

try

put the tryDepth -- 1

catch

put the tryDepth -- 0 (outside of the 'try' portion)

end try

Tech Talk

Syntax: tryDepth()

the tryDepth

 

This topic was last updated on October 11, 2019, at 11:18:54 AM.

Eggplant icon Eggplantsoftware.com | Documentation Home | User Forums | Support | Copyright © 2019 Eggplant