TRY...CATCH...FINALLY...END TRY

Description

Isolates code that can cause an exception, describes what to do if an exception of a given type is encountered, and allows you to close files or network connections (and return objects to their original state) whether or not an exception is encountered.

Syntax

TRY   trystatements
CATCH ( ThrowableType1 exIdentifier1 )
   catchstatements1
CATCH ( ThrowableType2 exIdentifier2 )
   catchstatements2
...
CATCH ( ThrowableTypeN exIdentifierN )
   catchstatementsN
FINALLY
   cleanupstatements
END TRY

Parameter

Description

trystatements

Block of code that might potentially throw an exception.

ThrowableTypeN

Object type of exception to be caught. A CATCH block is optional if you include a FINALLY block. You can include multiple CATCH blocks. Every CATCH block in a try-catch block must include a corresponding exception object type and a local variable of that type.

exIdentifierN

Local variable of type ThrowableTypeN.

catchstatementsN

Code to handle the exception being caught.

cleanupstatements

Cleanup code. The FINALLY block is optional if you include one or more CATCH block.


Usage

The TRY block, which is the block of statements between the TRY and CATCH keywords (or the TRY and FINALLY keywords if there is no CATCH clause), is used to isolate code that might potentially throw an exception. The statements in the TRY block are run unconditionally until either the entire block of statements is executed or some statement in the block causes an exception to be thrown.

Use a CATCH block or multiple CATCH blocks to handle exceptions thrown in a TRY block. In the event that an exception is thrown, execution of the TRY block is stopped and the statements in the first CATCH block are executed -- if and only if the exception thrown is of the same type or a descendant of the type of the identifier following the CATCH keyword.

If the exception thrown is not the same type or a descendant type of the identifier in the first CATCH block, the exception is not handled by this CATCH block. If there are additional CATCH blocks, they are evaluated in the order they appear. If the exception cannot be handled by any of the CATCH blocks, the statements in the FINALLY block are executed.

The exception then continues to unwind the call stack to any outer nested try-catch blocks. If there are no outer nested blocks, the SystemError event on the Application object is fired.

If no exception is thrown, execution continues at the beginning of the FINALLY block if one exists; otherwise, execution continues on the line following the END TRY statement.

FINALLY clause restriction

Do not use RETURN statements in the FINALLY clause of a TRY-CATCH block. This can prevent the exception from being caught by its invoker.

See also

THROW