Handling Java exceptions

Use JNI services to throw and catch Java™ exceptions.

Throwing an exception: Use one of the following services to throw a Java exception from a COBOL method:

  • Throw
  • ThrowNew

You must make the thrown object an instance of a subclass of java.lang.Throwable.

The Java virtual machine (JVM) does not recognize and process the thrown exception until the method that contains the call has completed and returned to the JVM.

Catching an exception: After you invoke a method that might have thrown a Java exception, you can do these steps:

  1. Test whether an exception occurred.
  2. If an exception occurred, process the exception.
  3. Clear the exception, if clearing is appropriate.

Use the following JNI services:

  • ExceptionOccurred
  • ExceptionCheck
  • ExceptionDescribe
  • ExceptionClear

To do error analysis, use the methods supported by the exception object that is returned. This object is an instance of the java.lang.Throwable class.

Example: handling Java exceptions