The TRY...CATCH...FINALLY...END-TRY structure is the basis for structured exception handling. The TRY block allows you to
test a section of code for exceptions that may occur during its execution. The CATCH block must specify the type of exception
you wish to handle if one should occur during the execution of the TRY block. The FINALLY block allows you to execute a section
of code following the TRY block, whether or not an exception occurred.
General Formats
Syntax Rules
- Identifier-1 must be an object reference of class (or a subclass of) System.Exception (.NET COBOL) or java.lang.Throwable
(JVM COBOL).
General Rules
- If identifier-1 is omitted, it is equivalent to specifying the special register EXCEPTION-OBJECT as identifier-1. EXCEPTION-OBJECT
has class System.Exception (.NET COBOL) or java.lang.Throwable (JVM COBOL).
- If an exception is raised during the execution of imperative-statement-1, each CATCH phrase is examined in the order specified
until one is found for which the class of the exception that was thrown is equal to, or derives from, the class of identifier-1.
If such an identifier-1 is found, identifier-1 is set to contain the value of the thrown exception, and the associated imperative-statement-2
is executed. Execution continues according to the rules for each statement specified in imperative-statement-2. If a control
branching or conditional statement that causes explicit transfer of control is executed, control is transferred in accordance
with the rules for that statement; otherwise, upon completion of the execution of imperative-statement-2, control is transferred
to imperative-statement 3, if specified, or to the end of the TRY statement.
- If no exception is raised during the execution of imperative-statement-1, execution continues according to the rules for each
statement specified in imperative-statement-1. If a control branching or conditional statement that causes explicit transfer
of control is executed, control is transferred in accordance with the rules for that statement; otherwise, upon completion
of the execution of imperative-statement-1, control is transferred to imperative-statement 3, if specified, or to the end
of the TRY statement.
- Execution of imperative-statement-3 continues according to the rules for each statement specified in imperative-statement-3.
If a control branching or conditional statement that causes explicit transfer of control is executed, control is transferred
in accordance with the rules for that statement; otherwise, upon completion of the execution of imperative-statement-3, control
is transferred to the end of the TRY statement
Example
The following is an example of specifying the EXCEPTION-OBJECT register:
try
set str to "some string"
catch
display exception-object::ToString()
finally
end-try