Skip to main content

I'm testing exception handling between a JVM COBOL class and a calling Java class.

An exception is raised in a JVM COBOL class, such as

raise new MyException("Blah blah")

When the exception is checked for in a try/catch block of a calling Java class, such as

        RaiseException test = new RaiseException();
        try {
            test.instanceMethod();
        }
        catch (MyException e) {
            System.out.println(e.getMessage());
        }

the compiler reports "This exception is never thrown from the try statement body".

Exceptions raised in  JVM COBOL do not seem to be recognised by calling Java programs. Not what I would expect. Am I missing something?

I'm testing exception handling between a JVM COBOL class and a calling Java class.

An exception is raised in a JVM COBOL class, such as

raise new MyException("Blah blah")

When the exception is checked for in a try/catch block of a calling Java class, such as

        RaiseException test = new RaiseException();
        try {
            test.instanceMethod();
        }
        catch (MyException e) {
            System.out.println(e.getMessage());
        }

the compiler reports "This exception is never thrown from the try statement body".

Exceptions raised in  JVM COBOL do not seem to be recognised by calling Java programs. Not what I would expect. Am I missing something?

This works fine for me using the following example:

Java main program:

public class mainjava {

	/**
	 * @param args
	 */
    public static void main(String[] args) {
	
        cobClass1 cob = new cobClass1();

        try {
            cob.testexception();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
        System.out.println("after exception!");
    }

}

COBOL JVM class:

       class-id cobClass1 public.

       working-storage section.

       method-id testexception.
       local-storage section.
       procedure division.
          declare myException as type Exception = 
             new Exception("Something is really wrong.").
          raise myException 
          goback.
       end method.
       
       end class.

I'm testing exception handling between a JVM COBOL class and a calling Java class.

An exception is raised in a JVM COBOL class, such as

raise new MyException("Blah blah")

When the exception is checked for in a try/catch block of a calling Java class, such as

        RaiseException test = new RaiseException();
        try {
            test.instanceMethod();
        }
        catch (MyException e) {
            System.out.println(e.getMessage());
        }

the compiler reports "This exception is never thrown from the try statement body".

Exceptions raised in  JVM COBOL do not seem to be recognised by calling Java programs. Not what I would expect. Am I missing something?

Chris,

and your example also works using

raise new Exception("Something..")

However, if you create your own sub-class of java.lang.Exception in JVM COBOL, which is what I am actually trying to raise in COBOL and catch in Java, it does not behave the same.

      class-id org.bulkhaul.cm.lang.exception.CmException public

          inherits type java.lang.Exception.

      working-storage section.

      method-id New.

      local-storage section.

      procedure division using by value msg as string.

          invoke super::New(msg)

          goback.

      end method.

      end class.


I'm testing exception handling between a JVM COBOL class and a calling Java class.

An exception is raised in a JVM COBOL class, such as

raise new MyException("Blah blah")

When the exception is checked for in a try/catch block of a calling Java class, such as

        RaiseException test = new RaiseException();
        try {
            test.instanceMethod();
        }
        catch (MyException e) {
            System.out.println(e.getMessage());
        }

the compiler reports "This exception is never thrown from the try statement body".

Exceptions raised in  JVM COBOL do not seem to be recognised by calling Java programs. Not what I would expect. Am I missing something?

Chris,

and your example also works using

raise new Exception("Something..")

However, if you create your own sub-class of java.lang.Exception in JVM COBOL, which is what I am actually trying to raise in COBOL and catch in Java, it does not behave the same.

      class-id org.bulkhaul.cm.lang.exception.CmException public

          inherits type java.lang.Exception.

      working-storage section.

      method-id New.

      local-storage section.

      procedure division using by value msg as string.

          invoke super::New(msg)

          goback.

      end method.

      end class.


I'm testing exception handling between a JVM COBOL class and a calling Java class.

An exception is raised in a JVM COBOL class, such as

raise new MyException("Blah blah")

When the exception is checked for in a try/catch block of a calling Java class, such as

        RaiseException test = new RaiseException();
        try {
            test.instanceMethod();
        }
        catch (MyException e) {
            System.out.println(e.getMessage());
        }

the compiler reports "This exception is never thrown from the try statement body".

Exceptions raised in  JVM COBOL do not seem to be recognised by calling Java programs. Not what I would expect. Am I missing something?

There does seem to be a problem with java recognizing these custom COBOL exceptions.

I would recommend that you create a support incident with Customer Care so that we can rpi this problem.

I find that you can catch the custom exception in java if you use Exception in the catch instead of the custom exception:.

       RaiseException test = new RaiseException();

       try {

           test.instanceMethod();

       }

       catch (Exception e) {

           System.out.println(e.getMessage());

       }