Catch

Specifies the code to execute if a value or error is thrown during execution of a try statement.

Catch ErrorClass as OutputVar
{
    Statements
}

Parameters

ErrorClass

Type: Class

The class of value that should be caught, such as Error, TimeoutError or MyCustomError. This can also be a comma-delimited list of classes. Classes must be specified by their exact full name and not an arbitrary expression, as the Prototype of each class is resolved at load time. Any built-in or user-defined class can be used, even if it does not derive from Error.

If no classes are specified, the default is Error.

To catch anything at all, use Catch Any.

A load-time error is displayed if an invalid class name is used, or if a class is inaccessible due to the presence of a local variable with the same name.

OutputVar

Type: Variable

The variable in which to store the thrown value, which is typically an Error object. This cannot be a dynamic variable.

If omitted, the thrown value cannot be accessed directly, but can still be re-thrown by using Throw with no parameter.

Statements

The statements to execute if a value or error is thrown.

Braces are generally not required if only a single statement is used. For details, see {...} (block).

Remarks

Multiple catch statements can be used one after the other, with each one specifying a different class (or multiple classes). If the value is not an instance of any of the listed classes, it is not caught by this try-catch, but might be caught by one further up the call stack.

Every use of catch must belong to (be associated with) a try statement above it. A catch always belongs to the nearest unclaimed try statement above it unless a block is used to change that behavior.

The parameter list may optionally be enclosed in parentheses, in which case the space or tab after Catch is optional.

Catch may optionally be followed by else, which is executed if no exception was thrown within the associated try block.

The One True Brace (OTB) style may optionally be used. For example:

try {
    ...
} catch Error {
    ...
}

Load-time errors cannot be caught, since they occur before the try statement is executed.

Try, Throw, Error Object, Else, Finally, Blocks, OnError

Examples

See Try.