Absent Member.
Absent Member.
2462 views

Exception handling

Jump to solution

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?

0 Likes
1 Solution

Accepted Solutions
Micro Focus Expert
Micro Focus Expert

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());

       }

View solution in original post

0 Likes
3 Replies
Micro Focus Expert
Micro Focus Expert

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.
0 Likes
Absent Member.
Absent Member.

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.

0 Likes
Micro Focus Expert
Micro Focus Expert

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());

       }

View solution in original post

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.