为什么这个异常没有被捕获

发布于 2024-11-03 22:58:23 字数 722 浏览 6 评论 0原文

我有以下代码,

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

但我忘记包含一个依赖于 jdom.jar 库的库。当我运行该应用程序时,我看到了这个错误。

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)

根据 JDOM 文档, newInsance() 方法会抛出 JDOMEXCeption ,所以它不应该捕获错误吗?

另外,如何避免添加 catch(Exception e) 子句以避免未知异常。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隱形的亼 2024-11-10 22:58:23

我忘记包含一个库
这是 jdom.jar 的依赖项
图书馆。当我运行应用程序时我
看到这个错误。

您在运行时错误中看到的错误,该错误是针对预期位于 CLASSPATH 中但未找到的类而引发的。如果 jdom.jar 确实包含 org/jaxen/NamespaceContext 类,那么应该可以解决此问题。

根据 JDOM 文档,
newInsance() 方法抛出一个
JDOMEXCeption 所以它不应该有
发现错误了吗?

不,这不是 JDOMException,而是 NoClassDefFoundError,因此它不会捕获它。 最重要的是,这发生在 JDOM 类出现之前 - 发生在类加载期间。

另外,我怎样才能避免添加
要避免的 catch(Exception e) 子句
未知异常

一般来说,您不应该尝试捕获 NoClassDefFoundError,因为它是一种属于无法恢复的失败类别的错误。您可以尝试通过使用反射并捕获 ClassNotFoundException 来解决这个问题,但正如我一般所说,这是一个您无法从中恢复的异常,因此尝试捕获它可能是一个没有实际意义的问题。

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.

叹倦 2024-11-10 22:58:23

此异常不是由构造函数引发的。它是由类加载器引发的。当它尝试在构造函数运行之前加载您的类时,找不到该类,并且引发了您尚未处理的异常(未找到类定义错误)(顺便说一句,无法处理错误)。

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).

娜些时光,永不杰束 2024-11-10 22:58:23

它没有被抓住,因为它没有被抛出。抛出的异常是java.lang.NoClassDefFoundError

并且如果你想捕获异常,你必须捕获它。您无法采取任何措施来避免这种情况,这会破坏整个例外点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文