Skip to main content

Problem:

Coding a DECLARATIVE section within a COBOL program.

Resolution:

A useful feature of the COBOL language is the DECLARATIVE section.  This section can be used to handle I/O errors when they occur rather than checking the status code after each I/O statement.  The following source sample illustrates how to code a DECLARATIVE.

        select testfile assign "testfile.dat"

           organization line sequential.

       fd  testfile.

       01  tf-record                   pic x(80).

       procedure division.

       declaratives.

       proc-decl section.

          use after error procedure on testfile.

             display "Error on TESTFILE."

> Add your code here to handle error exceptions.

             stop run.

       proc-decl-exit.

           exit.

       end declaratives.

           open input testfile.

> Control is passed to the DECLARATIVEs section if an error ocurrs when processing TESTFILE.

           close testfile.

           stop run.

Old KB# 4391