skip to main content

Procedure Division Statements : TRY

TRY
The TRY statement is used as an exception handler.
General Format
TRY Imperative-Statement-1
  [ CATCH {Exception-Class} Imperative-Statement-2 ] ...
  [ CATCH EXCEPTION Imperative-Statement-3 ]
  [ FINALLY Imperative-Statement-4 ]
  END-TRY
Syntax rules
1.
2.
Exception-Class is a User-defined word, as defined in the Definitions section in the Preface of this document.
3.
4.
5.
General rules
1.
Exception-Class must appear in the REPOSITORY Paragraph and reference a class of type java.lang.Trowable.
2.
3.
4.
5.
6.
7.
8.
Examples
The following code snippets are utilities that checks if a given file exists.
The first snippet catches the generic exception and uses EXCEPTION-OBJECT to advise the user of the error encountered:
       CONFIGURATION SECTION.
       REPOSITORY.
           CLASS JFILE AS "java.io.File"
       WORKING-STORAGE SECTION.
       77  W-STATUS PIC 9(9).
       88  FILE-NOTFOUND   VALUE 0.
       88  FILE-EXISTS     VALUE 1.
       88  ERROR-CONDITION VALUE 2.
       LINKAGE SECTION.        
       77  FILENAME PIC X(256).
       PROCEDURE DIVISION USING FILENAME.
             IF JFILE:>new(FILENAME):>exists()
                SET FILE-EXISTS TO TRUE
                SET FILE-NOTFOUND TO TRUE
           CATCH EXCEPTION
             SET ERROR-CONDITION TO TRUE
             DISPLAY MESSAGE EXCEPTION-OBJECT:>getMessage()
The second snippet catches the SecurityException thrown by the exists() method of java.io.File, as described in the method javadoc: http://download.oracle.com/javase/6/docs/api/java/io/File.html#exists() :
       CONFIGURATION SECTION.
       REPOSITORY.
           CLASS JFILE  AS "java.io.File"
           CLASS SECEXC AS "java.lang.SecurityException"
       WORKING-STORAGE SECTION.
       77  W-STATUS PIC 9(9).
       88  FILE-NOTFOUND   VALUE 0.
       88  FILE-EXISTS     VALUE 1.
       88  SECURITY-ERROR  VALUE 2.
       LINKAGE SECTION.        
       77  FILENAME PIC X(256).
       PROCEDURE DIVISION USING FILENAME.
             IF JFILE:>new(FILENAME):>exists()
                SET FILE-EXISTS TO TRUE
                SET FILE-NOTFOUND TO TRUE
             SET SECURITY-ERROR TO TRUE
             DISPLAY MESSAGE EXCEPTION-OBJECT:>getMessage()

Copyright (c) 2017 Veryant
Contact us
Please share your comments on this manual or on any
Veryant product documentation with the email button at the top left