Is it possible to use "throws" according to the method in an external JAR

I have a separate JAR library with a set of methods that throw custom exceptions, for example:

public String methodName() throws CustomException {
    // code here
}

Then I add the JAR to the classpath and reference the library method in the try statement in the source code:

try {
    DemoClass demoClass = new DemoClass ();
    demoClass.methodName() // this should throw a CustomException if something occurs
} catch (CustomException e) {
    // something here
}

The following compilation error is saved in the above code snippet:

A custom exception never throws itself into the body of the corresponding try statement

If the method is in a local context (not packaged in a JAR), the code works. So my question is: is it possible to "throw out" user exceptions from JAR libraries?

+5
source share
3 answers

, , . , , , - .

ParseException, java.text.ParseException, org.apache.commons.cli.ParseException, demoClass.methodName() , , . , , .

+1

, .

try {
    MyClass myClass = new MyClass();
    myClass.methodName() // this should throw a CustomException if something occurs
} catch (CustomException e) {
    // something here
}

, , "MyClass" "CustomException" "MyClass" "CustomException" .

+2

, CustomException , , DemoClass.methodName() ​​ . , , , .

, ( ) throws , .


MyClass customException JAR, ?

.

However, I suspect that you actually have two different (incompatible) versions of the corresponding class or interface in the two JARs. That would be a mistake. Even if you manage to assemble it, you are likely to run into problems with class loaders due to a signature conflict.

0
source

All Articles