如何在java中捕获嵌套异常 [英] How to catch a nested exception in java
问题描述
我在Apache Tomcat(v6.0.32)中使用Apache Xalan(v.2.7.1)将XML转换为XHTML。有时,客户端会取消加载,并抛出以下异常:
I'm using Apache Xalan (v.2.7.1) to translate XML to XHTML in Apache Tomcat (v6.0.32). Sometimes the loading gets cancelled by the client and the following exception is thrown:
javax.xml.transform.TransformerException: org.apache.xalan.xsltc.TransletException: ClientAbortException: java.io.IOException
at org.apache.xalan.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:636)
at org.apache.xalan.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:303)
...
我想捕获ClientAbortException异常,这样它不会垃圾日志。但是,如何检查异常是否嵌套在ClientAbortException内?我试过这样的东西:
I would like to catch the ClientAbortException-exception, so that it doesn't spam the log. However, how can I check if the exception is nested inside the ClientAbortException? I tried something like this:
...
} catch (Exception e) {
if (e.getCause() != null && e.getCause().getCause() instanceof org.apache.catalina.connector.ClientAbortException) {
//do nothing
} else {
throw e;
}
} finally {
...
但是只给我一个nullpointerexception,因为第一个getCause没有一个getCause。任何想法?
But it only gives me a nullpointerexception as the first getCause doesn't have a getCause. Any ideas?
推荐答案
使用 ExceptionUtils.getRootCause(Throwable)
在Apache Commons-lang中,它将遍历您的原因链。
Use the ExceptionUtils.getRootCause(Throwable)
method in Apache Commons-lang, it will traverse the cause chain for you.
这篇关于如何在java中捕获嵌套异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!