try
- keyword
- A try/catch block protects a block of code by catching any exceptions that are raised during the execution of that block.
- Implemented exception handling with the try...catch...end try control structure**,** and the throw command. This example shows how to use them:
on testExceptions
try
doSomething
throw "BadProblem"
doSomethingElse -- this will not get executed
catch anException
-- do processing here to handle the error
put anException -- shows "BadProblem"
end try
end testExceptions
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 textExceptions handler.
The exception variable name on the "catch" line (anException
in the example) is optional. If supplied, it receives the name of the exception which has been caught. There are also two functions which can be used to obtain information about the exception, exceptionName() andexceptionReason(). The exceptionName function returns the name of the exception which was thrown ("BadProblem" in the example), which is the same value the exception variable receives. The exceptionReason function returns some text which generally describes the reason for the exception in more detail. A reason may be given with the throw command by supplying it as a second parameter: throw "BadProblem","Something is seriously messed up!"
The value returned in a catch statement is an exception object (a property list with its objectType set to "exception", and "name", "reason", "location", “callStack”, and "asText" properties), rather than just the exception name. The name, reason, and location properties of this value are the same as the values returned by the exceptionName(), exceptionReason() and exceptionLocation() functions, which are considered obsolete. The asText property is set to the same value as the name, so displaying the exception will show that information.
The catch portion of a try block is optional. If omitted, any exceptions encountered while executing the statements in the block will prevent the remainder of statements within that try block from being executed, but will otherwise be ignored, and execution will continue following the try block.
There is also a single-line variation of try that automatically catches any exceptions thrown by a single statement. If an exception is caught, it is stored in the global property the exception which is otherwise set to empty:
try to set x to 12*y -- (will fail if y is not a number)
if the exception is not empty then LogError the exception
- Related: throw, exception, exceptionName, exceptionReason, exceptionLocation, tryDepth