Skip to main content
Version: 23.3

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:

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

In this example, the throw command within the try block will throw an exception. That will cause the flow of execution to be interrupted and transferred immediately to the first statement after the catch statement. The exception that was thrown (in the form of an exception object, as described here) will be assigned to the anException variable.

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, that exception would interrupt execution before the throw command in this block of code, and would be caught by the catch block here.

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.

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.

In SenseTalk, unlike some other languages, there is only a single catch allowed for each try block, which catches any exceptions thrown within that block. In the following example, only "Unknown Property or Function" exceptions are dealt with (by merely incrementing a global counter). Any other exceptions are thrown again, to be handled by a try block at a higher level (or not).

Example:

try
doThingsHere
catch except
if except.name is "Unknown Property or Function" then
add 1 to global unknownCallCounter
else
throw except // re-throw any other exception
end if
end try

The single-line 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.

Example:

set amt to "plenty"
try to set product to 5 * amt -- throws an exception because amt is not a number
if the exception is not empty then put the exception -- displays the exception

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