什么是抑制异常? [英] What is a suppressed exception?

查看:164
本文介绍了什么是抑制异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

soc )) / 3616483 / why-did-the-jvm-still-not-support-tail-call-optimization / 3659402#3659402> answer to 关于尾部调用优化的一个问题提到Java 7有一个称为抑制异常的新功能,因为添加ARM(支持ARM CPU?)。

A comment (by user soc) on an answer to a question about tail call optimisation mentioned that Java 7 has a new feature called "suppressed exceptions", because of "the addition of ARM" (support for ARM CPUs?).

在此上下文中是什么抑制异常?在其他情况下,被禁止的异常将被捕获,然后被忽略(很少是一个好主意);这显然是一个不同的东西。

What is a "suppressed exception" in this context? In other contexts a "suppressed exception" would be an exception that was caught and then ignored (rarely a good idea); this is clearly something different.

推荐答案

我相信评论者指的是一个异常,在被抛出内部时被半忽略终止 尝试使用资源阻止在尝试块中抛出现有异常的上下文中:

I believe the commenter is referring to is an exception which is semi-ignored when it's thrown within the implicit finally block of a try-with-resources block, in the context of an existing exception being thrown from the try block:


可以从与try-with-resources语句关联的代码块抛出异常。在示例writeToFileZipFileContents中,可以从try块抛出异常,并且当尝试关闭ZipFile和BufferedWriter对象时,最多可以从try-with-resources语句抛出两个异常。如果从try块抛出异常,并从try-with-resources语句中抛出一个或多个异常,那么从try-with-resources语句抛出的这些异常被抑制,并且块抛出的异常是一个这是由writeToFileZipFileContents方法抛出的。您可以通过从try块抛出的异常调用Throwable.getSuppressed方法来检索这些抑制的异常。

An exception can be thrown from the block of code associated with the try-with-resources statement. In the example writeToFileZipFileContents, an exception can be thrown from the try block, and up to two exceptions can be thrown from the try-with-resources statement when it tries to close the ZipFile and BufferedWriter objects. If an exception is thrown from the try block and one or more exceptions are thrown from the try-with-resources statement, then those exceptions thrown from the try-with-resources statement are suppressed, and the exception thrown by the block is the one that is thrown by the writeToFileZipFileContents method. You can retrieve these suppressed exceptions by calling the Throwable.getSuppressed method from the exception thrown by the try block.

(这是引用一个部分从链接页面称为抑制异常。)

(That's quoting a section called "Suppressed Exceptions" from the linked page.)

这篇关于什么是抑制异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆