Handling Java Exceptions

Restriction: This applies to native code only.

An exception thrown by Java is passed back to COBOL as a native OO COBOL exception raised against the javaexpt class. The default exception behavior is for the COBOL run-time system to display a message warning of the exception, and then terminate. Alternatively, you can trap the exception by adding an exception handler to your COBOL program.

To trap Java exceptions:

  1. Declare the Exceptionmanager, JavaExceptionManager, and Callback or EntryCallback classes in the Class-Control paragraph. For example:
    repository.
        ...
        class JavaExceptionManager as "javaexpt"
        class ExceptionManager as "exptnmgr"
        class Callback as "callback"
        class EntryCallback as "entrycll"
        ...
    Tip: If you declare your own exception handling class method or entry-point, be aware that if it shares its name with one of the default Micro Focus exception handling methods shown above, you will not be able to invoke that default method.
  2. Write an exception handler (either a method in a class, or an entry-point) and create a Callback or EntryCallback to it. For example, a Callback looks like this:
    invoke Callback "new" using anObject z"methodName"
                      returning aHandler

    An EntryCallback looks like this:

    invoke EntryCallback "new" using  z"entryPointname"
                      returning aHandler
  3. Register your callback against the JavaExceptionManger class. For example:
    invoke ExceptionManager "register"
             using JavaExceptionManager aHandler

Now all Java exceptions thrown by classes you are calling through the native OO COBOL Java domain get sent to your exception handler.

This is an example of a COBOL program which catches an exception thrown by a Java program:

$set ooctrl (+p-f)
 program-id. ExceptionCatcher.

 class-control.
     SimpleClass is class "$JAVA$SimpleClass"
     EntryCallback is class "entrycll"
     JavaExceptionManager is class "javaexpt"
     ExceptionManager is class "exptnmgr"
     .

 working-storage section.
 01 theInstance                  object reference.
 01 wsCallback                   object reference.
 local-storage section.
 01 filler pic x.   *> dummy storage to allow the local entry
                    *> point to be used for the callback
 linkage section.
 01 lnkException                 object reference.

 procedure division.
*>---Set up Exception handler
     invoke EntryCallback "new" using z"JException"
                            returning wsCallback
     invoke ExceptionManager "register" 
                                  using javaexceptionmanager
                                        wsCallback
*>---Instantiate the class
     invoke SimpleClass "new" returning theInstance

     display "instantiated"
     invoke theInstance "TestException"
     display "excepted"
     stop run.


 entry "Jexception" using lnkException.
     invoke lnkException "display"
     .

The Local-Storage Section in the above program is simply to enable recursion - the COBOL run-time system treats the invocation of the EntryCallback as a recursive call. Without a Local-Storage Section, this will result in a run-time system error. This is the Java code for SimpleClass:

import java.lang.* ;

public class SimpleClass {

  public SimpleClass() {
  }

  public void TestException() throws Exception
  {
     Exception e = new Exception ("Test error" );
     throw e;
  }
}

See the Java Run-time Class Library Reference, which is in help\mfcobol.docs.zip in your COBOL development system installation.