当最终抛出异常时,为什么Java不支持从try / catch丢失的异常中检索异常? [英] Why does Java not support retrieval of exceptions from try/catch lost when an exception is thrown from finally?

查看:165
本文介绍了当最终抛出异常时,为什么Java不支持从try / catch丢失的异常中检索异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java 7中,该功能已添加到(通过 getSuppressed())以获取try-with-resources语句的隐式finally块引发的异常。

In Java 7, the feature was added to (via getSuppressed()) get exceptions thrown from the implicit finally block of a try-with-resources statement.

(我所知道的)似乎仍然没有办法做相反的事情-当有一个显式的finally块并且抛出异常时,掩盖从try / catch引发和挂起的异常。

There still doesn't seem to be a way (that I know of) to do the opposite - when there is an explicit finally block and that throws an exception, masking the exceptions thrown and pending from the try/catch.

为什么Java不提供通过类似于 getSuppressed()的机制来获取这些隐藏/丢失的异常的功能?

Why does Java not provide functionality to get these buried/lost exceptions through a mechanism similar to getSuppressed()?

该功能的实现似乎与 getSuppressed()或链接的异常中使用的实现类似,并且所提供的好处将非常有用,但仍会在每个发行版中均被忽略。

It would seem that the implementation of this functionality would be similar to that used in getSuppressed() or chained exceptions, and the provided benefit would be very useful, yet it continues to be left out of each release.

通过类似于 getSuppressed()的方法调用将这些屏蔽的异常提供给程序员,会有什么危险? ?

What would be the danger of making these masked exceptions available to programmers through a method call similar to getSuppressed()?

(如果该功能已经存在,我很无能为力,请提前道歉。)

(Apologies in advance if this functionality already exists and I'm just clueless.)

推荐答案

抑制事物不仅限于尝试资源,而且您可以自己将其用于类似情况。例如,为其他情况提供了

The suppression thing isn't limited to try-with-resources, and you can use it for similar situations yourself. E.g., it is provided for other situations.

try-with-resources将关闭资源的逻辑置于幕后,所以您不必您无法直接访问自己的代码来处理该过程中发生的任何异常。因此,他们添加了抑制功能,以便可以在幕后代码中使用它。

try-with-resources puts the logic for closing the resources behind the scenes, so you don't have direct access in your own code to dealing with any exceptions that occur during the process. So they added the "suppression" thing so they could use it in that behind-the-scenes code.

但是,聪明的是,他们并不仅是 使其可以在此处使用。您可以通过 Throwable#addSuppressed

But cleverly, they didn't only make it something that could be used there. You can use it yourself, via Throwable#addSuppressed.

您可以在给出的伪代码示例中看到它在 JLS§14.20.3.1;这是它的真实代码版本:

You can see this in the pseudo-code example given in JLS §14.20.3.1; here's a real code version of it:

{
    SomeResource someResource = null;
    Throwable primaryException = null;

    try {
        someResource = /*...get the resource...*/;
        /*...do something...*/
    }
    catch (Throwable t) {
        primaryException = t;
        throw t;
    }
    finally {
        if (someResource != null) {
            if (primaryException != null) {
                // Dealing with a primary exception, close the resource
                // and suppress any exception resulting
                try {
                    someResource.close();
                }
                catch (Throwable suppressed) {
                    primaryException.addSuppressed(suppressed);
                }
            }
            else {
                // Not dealing with a primary exception, close the
                // resource without suppressing any resulting exception
                someResource.close();
            }
        }
    }
}

这篇关于当最终抛出异常时,为什么Java不支持从try / catch丢失的异常中检索异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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