3

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ziggy
  • 15,677
  • 67
  • 194
  • 287

3 Answers3

4

This exception is not raised by the constructor. It's raised by the class loader. When it tries to load your class long before the constructor runs, the class was not found and this exception (No Class Defination Found Error) was raised which you have not handled (BTW errors can't be handled).

S M Kamran
  • 4,423
  • 7
  • 25
  • 35
4

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.

CoolBeans
  • 20,654
  • 10
  • 86
  • 101
1

It wasn't caught because it wasn't thrown. The exception thrown was a java.lang.NoClassDefFoundError

And if you want to catch an Exception, you have to catch it. There is nothing you can do to avoid this, that would kind of defeat the whole point of exceptions.

Richard H
  • 38,037
  • 37
  • 111
  • 138
  • Hi, according to the documentation, JDOMException extends "Exception" so shouldnt it have been caught because NoClassDefFoundError also extends "Exception" ? – ziggy Apr 30 '11 at 15:56
  • Parent type can catch any child type but vice versa is not possible – S M Kamran Apr 30 '11 at 15:58
  • ok does this mean that to catch these unkownn exceptions i have to add catch(Exception e) at the end of the try/catch clause? – ziggy Apr 30 '11 at 15:58
  • No on two counts. 1) The catch will only catch exceptions of type JDOMException and any subclasses. NoClassDefFoundError is not a sub-class of JDOMException. 2) The exception isn't been thrown here anyway, it's been thrown by the classloader. – Richard H Apr 30 '11 at 15:59
  • NoClassDefFoundError doesn't extend "Exception". But even if it did, it wouldn't have been caught in a `catch JDOMException` clause. It wouldn't be caught in a `catch Exception` clause either... – Don Roby Apr 30 '11 at 15:59
  • @Ziggy: you shouldn't be catching exceptions like NoClassDefFoundError (if even that is possible) - your program won't work without it. – Richard H Apr 30 '11 at 16:00
  • @Ziggy: Only child class of Exception can be catchable. – S M Kamran Apr 30 '11 at 16:02
  • @ziggy, you have an incorrect understanding of the Exception hierarchy. See http://download.oracle.com/javase/tutorial/essential/exceptions/ – Thorbjørn Ravn Andersen Apr 30 '11 at 16:07
  • Ok i get it. I know which library was missing i just wanted to understand why it happened. Thank you all for your assistance. – ziggy Apr 30 '11 at 16:10