检查某种异常类型是否是嵌套异常中的原因(原因等......)的最佳方法?
我正在编写一些 JUnit 测试来验证是否引发了 MyCustomException
类型的异常。 然而,该异常多次包装在其他异常中,例如,在 InvocableTargetException 中,而后者又被包装在 RuntimeException 中。
确定 MyCustomException 是否以某种方式导致我实际捕获的异常的最佳方法是什么? 我想做这样的事情(见下划线):
try { doSomethingPotentiallyExceptional(); fail("Expected an exception."); } catch (RuntimeException e) { if (!e.
wasCausedBy(MyCustomException.class) fail("Expected a different kind of exception."); }
我想避免调用 getCause()
几个“层”深,以及类似的丑陋的解决方法。 有更好的方法吗?
显然,Spring 有 NestedRuntimeException。 contains(Class),它可以满足我的需求 - 但我没有使用 Spring。
I am writing some JUnit tests that verify that an exception of type MyCustomException
is thrown. However, this exception is wrapped in other exceptions a number of times, e.g. in an InvocationTargetException, which in turn is wrapped in a RuntimeException.
What's the best way to determine whether MyCustomException somehow caused the exception that I actually catch? I would like to do something like this (see underlined):
try { doSomethingPotentiallyExceptional(); fail("Expected an exception."); } catch (RuntimeException e) { if (!e.
wasCausedBy(MyCustomException.class) fail("Expected a different kind of exception."); }
I would like to avoid calling getCause()
a few "layers" deep, and similar ugly work-arounds. Is there a nicer way?
Apparently, Spring has NestedRuntimeException.contains(Class), which does what I want - but I'm not using Spring.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您使用 Apache Commons Lang,则可以使用以下内容:
(1) 当原因应完全属于指定类型
时 (2) 当原因应该是指定类型或其子类类型
If you are using Apache Commons Lang, then you can use the following:
(1) When the cause should be exactly of the specified type
(2) When the cause should be either of the specified type or its subclass type
为什么要避免
getCause
。 当然,您可以自己编写一个方法来执行任务,例如:Why would you want to avoid
getCause
. You can, of course, write yourself a method to perform the task, something like:我认为你别无选择,只能通过 getCause 的层层调用。 如果您查看您提到的 Spring NestedRuntimeException 的源代码,这就是它的实现方式。
I don't think you have any choice but to call through the layers of getCause. If you look at the source code for the Spring NestedRuntimeException that you mention that is how it is implemented.
模仿是最真诚的奉承形式。 基于快速检查来源,这正是 NestedRuntimeException 所做的:
警告:上面是截至 2009 年 3 月 4 日的代码,因此,如果您确实想知道 Spring 现在在做什么,您应该研究当前存在的代码(无论何时)。
Imitation is the sincerest form of flattery. Based on a quick inspection of the source, this is exactly what NestedRuntimeException does:
CAVEAT: The above is the code as of 4 March 2009 so, if you really want to know what Spring is doing right now, you should research the code as it exists today (whenever that is).
您可以使用番石榴来做到这一点:
You can do this using guava:
根据 Patrick Boos 的回答:
如果您使用 Apache Commons Lang 3,您可以检查:
indexOfThrowable:返回异常链中与指定类(完全)匹配的第一个 Throwable 的(从零开始的)索引。 指定类的子类不匹配
或
indexOfType:返回与指定类或子类匹配的第一个Throwable的(从零开始的)索引在异常链中。 指定类的子类匹配
Java 8 的多种类型示例:
Based on Patrick Boos answer:
If you using Apache Commons Lang 3 you can check:
indexOfThrowable: Returns the (zero based) index of the first Throwable that matches the specified class (exactly) in the exception chain. Subclasses of the specified class do not match
or
indexOfType: Returns the (zero based) index of the first Throwable that matches the specified class or subclass in the exception chain. Subclasses of the specified class do match
Example for multiple types with Java 8:
您可以使用 Apache Commons Lang 库中的 ExceptionUtils.hasCause(ex, type) 。
You can use
ExceptionUtils.hasCause(ex, type)
from the Apache Commons Lang library.好吧,我认为如果不调用 getCause() 就没有办法做到这一点。 如果您认为它很丑陋,请实现一个实用程序类来执行此操作:
Well, I think there's no way to do this without calling
getCause()
. It you think it's ugly implement a utility class for doing this:如果感兴趣的异常肯定是“根本”原因,则
assertj
是查找getRootCause
检查的另一个位置,尽管从今天的源代码来看,它似乎有可能其他答案中讨论了无限循环问题。If the exception of interest is definitely the "root" cause,
assertj
is an additional place to find agetRootCause
check, although from the source today, it appears to have the possible infinite loop problem discussed in other answers.