Previous Topic Next topic Print topic


TRY Statement

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.

try-statement

Example of a local declaration of an exception

      *>   The example below shows that you can declare e at the point of use.
      *>   It is in scope until the end-try.
           try
               display "exception test"
           catch e as type Exception
               display e::Message
           end-try

See also the Local Variables sample, available from Start > All Programs > Micro Focus Enterprise Developer > Samples > Visual COBOL Samples, under COBOL for JVM.

Further Information

In the TRY statement, the:

TRY statement-block
specifies the section of code to be tested for exceptions that may occur during its execution
CATCH statement-block
specifies the type of exception to handle if an exception occurs during execution of the TRY block
FINALLY statement-block
specifies the section of code to execute following the TRY block, whether or not an exception occurred

Your program can raise an exception using the RAISE statement and pass this exception back to the caller. This is commonly referred to as "throwing an exception".

Previous Topic Next topic Print topic