当第一个策略和恢复策略都失败时,Java中的异常组合
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了当第一个策略和恢复策略都失败时,Java中的异常组合,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3465字,纯文字阅读大概需要5分钟。
内容图文
![当第一个策略和恢复策略都失败时,Java中的异常组合](/upload/InfoBanner/zyjiaocheng/721/c442d017920c4d36a049dd999d98fd2f.jpg)
假设我有一个可能因检查异常而失败的方法(根据Sun的建议检查,因为它是可恢复的).
此方法失败,并触发恢复策略.
但是初始方法和恢??复策略都失败了.
在某些情况下,我可能想要两个堆栈跟踪,以便我知道为什么初始策略和恢复策略都失败了,而不仅仅是最后一个.
我能做什么?
我应该创建一个CompositeException类型或类似的东西?这是一个好习惯吗?
解决方法:
Java 7引入了抑制异常的概念.例如,try-with-resources语句是specified:
Resources are closed in the reverse order from that in which they were initialized. A resource is closed only if it initialized to a non-null value. An exception from the closing of one resource does not prevent the closing of other resources. Such an exception is suppressed if an exception was thrown previously by an initializer, the try block, or the closing of a resource.
和
If the initialization of the resource completes normally, and the try block completes abruptly because of a throw of a value V, then:
- If the automatic closing of the resource completes abruptly because of a throw of a value V2, then the try-with-resources statement completes abruptly because of a throw of value V with V2 added to the suppressed exception list of V.
这使用java.lang.Throwable.addSuppressedException,其javadoc读取:
Appends the specified exception to the exceptions that were suppressed in order to deliver this exception. This method is thread-safe and typically called (automatically and implicitly) by the try-with-resources statement.
The suppression behavior is enabled unless disabled via a constructor. When suppression is disabled, this method does nothing other than to validate its argument.
Note that when one exception causes another exception, the first exception is usually caught and then the second exception is thrown in response. In other words, there is a causal connection between the two exceptions. In contrast, there are situations where two independent exceptions can be thrown in sibling code blocks, in particular in the try block of a try-with-resources statement and the compiler-generated finally block which closes the resource. In these situations, only one of the thrown exceptions can be propagated. In the try-with-resources statement, when there are two such exceptions, the exception originating from the try block is propagated and the exception from the finally block is added to the list of exceptions suppressed by the exception from the try block. As an exception unwinds the stack, it can accumulate multiple suppressed exceptions.
An exception may have suppressed exceptions while also being caused by another exception. Whether or not an exception has a cause is semantically known at the time of its creation, unlike whether or not an exception will suppress other exceptions which is typically only determined after an exception is thrown.
Note that programmer written code is also able to take advantage of calling this method in situations where there are multiple sibling exceptions and only one can be propagated.
最后一段似乎适用于您的情况.所以你可以这样做:
public static void main(String[] args) throws Exception {
try {
throw new RuntimeException("Not now!");
} catch (Exception e) {
try {
tryAgain();
} catch (Exception e2) {
e.addSuppressed(e2);
throw e;
}
}
}
然后,堆栈跟踪将包含两个异常:
Exception in thread "main" java.lang.RuntimeException: Not now!
at tools.Test.main(Test.java:12)
Suppressed: java.lang.RuntimeException: I'm on holiday.
at tools.Test.tryAgain(Test.java:7)
at tools.Test.main(Test.java:15)
但调用者只能捕获主要异常.
内容总结
以上是互联网集市为您收集整理的当第一个策略和恢复策略都失败时,Java中的异常组合全部内容,希望文章能够帮你解决当第一个策略和恢复策略都失败时,Java中的异常组合所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。