如何在java中捕获嵌套异常

编程入门 行业动态 更新时间:2024-10-24 01:56:09
本文介绍了如何在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中捕获嵌套异常

本文发布于:2023-10-04 05:12:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1467123.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   异常   如何在   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!