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
|
1 |
TRY<br>   <span>trystatements<br></span>CATCH ( <span>ThrowableType1</span> <span>exIdentifier1</span> )<br>   <span>catchstatements1</span><br>CATCH ( <span>ThrowableType2</span> <span>exIdentifier2</span> )<br>   <span>catchstatements2</span><br>...<br>CATCH ( <span>ThrowableTypeN</span> <span>exIdentifierN</span> )<br>   <span>catchstatementsN</span><br>FINALLY<span><br>   cleanupstatements</span><br>END TRY |
|
Parameter |
Description |
|---|---|
|
trystatements |
Block of code that might potentially |
|
ThrowableTypeN |
Object type of exception to be caught. |
|
exIdentifierN |
Local variable of type ThrowableTypeN. |
|
catchstatementsN |
Code to handle the exception being caught. |
|
cleanupstatements |
Cleanup code. The FINALLY 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.