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 Statements

Behavior: 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.

Syntax:

try

statementsToTry

{

catch{exceptionVariable}

statementsToHandleErrors

}

end [try | catch]

try {to} singleStatementToTry

Example:

to testExceptionstry

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

Essentially, 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 Command

Behavior: 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.

Syntax:

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

throw {exception} exceptionObject

Tech Talk

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.

Examples:

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

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

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

Behavior: 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.

Syntax:

tryDepth()

the tryDepth

Example:

try

put the tryDepth -- 1

catch

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

end try

 

This topic was last updated on August 19, 2021, at 03:30:51 PM.

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