Skip to main content

We are in the process of migrating from Native COBOL to JVM Cobol but that is a long process.  We have a situation in prod where we need to signal a problem from several call levels down all the way back to the top level.  In Jvm COBOL, we can use Raise (the equivalent of Throw in Java) to signal this and Catch the situation at the top level.

Is there any way to do something like this with NATIVE COBOL.

JVM Cobol Example

Top Level Program

       $set ooctrl"+p-f"
       $set SOURCEFORMAT"VARIABLE"
       program-id. aptest01.

       environment division.
       configuration section.

       repository.
           class Ex as "java.lang.Exception"
           .


       data division.
       working-storage section.

       procedure division.
       1000-main.

            TRY
               call 'aptest02' using 'A'
               call 'aptest02' using 'B'
               call 'aptest02' using 'C'
            CATCH e as Ex
               display 'Exception'
            END-TRY.

           goback.
Called Program
       $set ooctrl"+p-f"
       $set SOURCEFORMAT"VARIABLE"
       program-id. aptest02.

       environment division.
       configuration section.

       repository.
           class Ex as "java.lang.Exception"
           .

       data division.
       working-storage section.
       77  e    Ex.

       linkage section.
       77  prm  pic x.

       procedure division using prm.

           display prm.

           if prm = 'B'
               invoke Ex "new" using "Test Exception" returning e
               raise e
           end-if.

           goback.

We are in the process of migrating from Native COBOL to JVM Cobol but that is a long process.  We have a situation in prod where we need to signal a problem from several call levels down all the way back to the top level.  In Jvm COBOL, we can use Raise (the equivalent of Throw in Java) to signal this and Catch the situation at the top level.

Is there any way to do something like this with NATIVE COBOL.

JVM Cobol Example

Top Level Program

       $set ooctrl"+p-f"
       $set SOURCEFORMAT"VARIABLE"
       program-id. aptest01.

       environment division.
       configuration section.

       repository.
           class Ex as "java.lang.Exception"
           .


       data division.
       working-storage section.

       procedure division.
       1000-main.

            TRY
               call 'aptest02' using 'A'
               call 'aptest02' using 'B'
               call 'aptest02' using 'C'
            CATCH e as Ex
               display 'Exception'
            END-TRY.

           goback.
Called Program
       $set ooctrl"+p-f"
       $set SOURCEFORMAT"VARIABLE"
       program-id. aptest02.

       environment division.
       configuration section.

       repository.
           class Ex as "java.lang.Exception"
           .

       data division.
       working-storage section.
       77  e    Ex.

       linkage section.
       77  prm  pic x.

       procedure division using prm.

           display prm.

           if prm = 'B'
               invoke Ex "new" using "Test Exception" returning e
               raise e
           end-if.

           goback.

The TRY/CATCH syntax is only supported in managed COBOL for .NET or JVM.

There are a couple ways in native OO-COBOL to implement exception handling but none that will support the kind of simple syntax that you wish to use and none that really works with procedural COBOL. These are not documented in the standard docs as they are holdovers from Net Express/Server Express. The documentation for these classes can be found in the folder Visual COBOL\\help in various files.