Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was this exception not caught

I have the following code

try {
   xpathInstance = XPath.newInstance(xpathExpr);
       list = (Text) xpathInstance.selectSingleNode(doc);
} catch (JDOMException e) {
   throw new Exception(e);
}

I had forgotten to include a library that was a dependency of the jdom.jar library. When i run the application i saw this error.

Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/NamespaceContext
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:141)
at org.jdom.xpath.XPath.newInstance(XPath.java:134)
at com.myapp.parser.GenericXMLParser.getSingleNodeValue(GenericXMLParser.java:63)

According to the JDOM documentation, the newInsance() method throws a JDOMEXCeption so shouldnt it have caught the error?

Also, how can i avoid having to add a catch(Exception e) clause to avoid unknown exceptions.

Thanks

like image 846
ziggy Avatar asked Jul 01 '26 13:07

ziggy


1 Answers

I had forgotten to include a library that was a dependency of the jdom.jar library. When i run the application i saw this error.

The error that you saw in a runtime error thrown for a class that was expected to be in the CLASSPATH but was not found. If jdom.jar does indeed include org/jaxen/NamespaceContext class then that should fix this issue.

According to the JDOM documentation, the newInsance() method throws a JDOMEXCeption so shouldnt it have caught the error?

No this is not a JDOMException, it's a NoClassDefFoundError, therefore it does not catch it. Most importantly, this happens before JDOM class is in the picture - happens during class loading.

Also, how can i avoid having to add a catch(Exception e) clause to avoid unknown exceptions

In general you should not try to catch NoClassDefFoundError since it is a type of error that falls under the category of failures from which recovery is not feasible. You can try to work around it by using Reflection and catching ClassNotFoundException but as I said in general this is an exception you cannot recover from so attempts to catch it is probably a moot point.

like image 94
CoolBeans Avatar answered Jul 03 '26 02:07

CoolBeans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!