try-with-resource中的close()异常 [英] Exception coming out of close() in try-with-resource

查看:384
本文介绍了try-with-resource中的close()异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读JDK7中的try-with-resource,当我考虑升级我的应用程序以使用JDK7运行时,我遇到了这个问题..

I was reading about the try-with-resource in JDK7 and while I was thinking of upgrading my application to run with JDK7 I faced this problem..

使用BufferedReader,例如写入抛出IOException和close抛出IOException ..在catch块中我关注的是写入抛出的IOException ..但我不会太在意关闭抛出的那个..

When using a BufferedReader for example the write throws IOException and the close throws IOException.. in the catch block I am concerned in the IOException thrown by the write.. but I wouldn't care much about the one thrown by the close..

数据库连接和任何其他资源的问题..

Same problem with database connections.. and any other resource..

作为一个例子,我创建了一个自动closeable resource:

As an example I've created an auto closeable resource:

public class AutoCloseableExample implements AutoCloseable {

    public AutoCloseableExample() throws IOException{
        throw new IOException();
    }

    @Override
    public void close() throws IOException {
        throw new IOException("An Exception During Close");
    }

}

现在使用时:

public class AutoCloseTest {

    public static void main(String[] args) throws Exception {
        try (AutoCloseableExample example = new AutoCloseableExample()) {
            System.out.println(example);

            throw new IOException("An Exception During Read");
        } catch (Exception x) {
            System.out.println(x.getMessage());
        } 
    }

}

怎么能我不必为BufferedReader这样的类创建包装器来区分这些异常吗?

how can I distinguish between such exceptions without having to create wrappers for classes such as BufferedReader?

大多数情况下我把资源关闭在finally块中的try / catch中而不关心很多关于处理它。

Most of cases I put the resource close in a try/catch inside the finally block without caring much about handling it.

推荐答案

让我们考虑一下这个类:

Lets consider the class:

public class Resource implements AutoCloseable {

    public Resource() throws Exception {
        throw new Exception("Exception from constructor");
    }

    public void doSomething() throws Exception {
        throw new Exception("Exception from method");
    }

    @Override
    public void close() throws Exception {
        throw new Exception("Exception from closeable");
    }
}

和try-with-resource块:

and the try-with-resource block:

    try(Resource r = new Resource()) {
        r.doSomething();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

1。启用所有3个抛出语句。

1. All 3 throw statements enabled.

消息构造函数的异常将被打印,构造函数抛出的异常将被抑制,这意味着你无法捕获它。

Message "Exception from constructor" will printed and the exception thrown by constructor will be suppressed, that means you can't catch it.

2。删除了构造函数。

现在将打印堆栈跟踪方法异常和抑制:从可关闭的例外下面。在这里你也无法捕捉close方法抛出的被抑制异常,但是你将被禁止有关被抑制的异常。

Now the stack trace will print "Exception from method" and "Suppressed: Exception from closeable" below. Here you also can't catch the suppressed exception thrown by close method, but you will be nofitied about the suppressed exception.

3。引发从构造函数和方法中删除。

3. Throws from constructor and method are removed.

您可能已经猜到了可关闭的异常将被打印。

As you have probably already guessed the "Exception from closeable" will be printed.

重要提示:在上述所有情况下,您实际上捕获所有异常,无论他们被扔到哪里。因此,如果你使用try-with-resource块你不需要用另一个try-catch包装块,它就没用了。

Important tip: In all of above situations you are actually catching all exceptions, no matter where they were throwed. So if you use try-with-resource block you don't need to wrap the block with another try-catch, it's simply useless.

希望它有帮助:)

这篇关于try-with-resource中的close()异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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