Handling errors in Java-interoperable COBOL applications

This topic provides suggestions on COBOL/Java™ interoperability error handing.

For cases where COBOL calls out to a static Java method using the CALL statement, exceptions can occur on the Java side of the application or in the Java call stub program. All exceptions occurring during the call can be handled using the ON EXCEPTION phrase of the CALL statement. If an exception occurs and the ON EXCEPTION phrase is specified, the exception will be cleared before the code in the ON EXCEPTION phrase is executed. If desired, user exception handling code can access the Java exception object that caused the exception through the IGY-JAVAIOP-CALL-EXCEPTION special register. To propagate the exception up the chain of calling programs, the JNI function Throw() can be used to re-throw the exception object. If an exception occurs and the ON EXCEPTION phrase is not specified, the COBOL run unit will be stopped due to the unhandled exception. An alternative method of handling exceptions that occur on the Java side of the application when COBOL calls a static Java method is for the static Java method to catch any exceptions and convert them to a return code that is returned to the COBOL caller. On the COBOL side, the COBOL application can decide what to do based on the return code.

For cases where Java calls COBOL, if a Java exception occurs in the native method call stub program when executing JNI functions, the stub program will not clear the exception and will immediately return to the Java side of the application where the exception can be caught and handled appropriately. If the exception is not handled, the Java application will be terminated due to the unhandled exception.