【问题标题】:Flow Control in try-with-resources close exceptiontry-with-resources 关闭异常中的流控制
【发布时间】:2018-05-09 17:32:50
【问题描述】:

我无法通过 Google 搜索找到答案,所以我在这里询问(寻求导航帮助)。 如果要在 try-with-resources 块中返回一个值,则 close 方法会抛出异常,我处理异常而不抛出,并恢复执行,是我尝试返回的值,还是在 catch 后恢复执行堵塞?例如:

public static int test(){
    class Foo implements AutoCloseable{
        @Override
        public void close(){
            throw new RuntimeException();
        }
    }
    try(Foo foo = new Foo()){
        return 1;
    }
    catch (RuntimeException e){
        //handle exception without throwing
    }
    return 2;
}

【问题讨论】:

标签: java try-with-resources


【解决方案1】:

异常抛出导致执行到达catch语句,因此返回2
它与close() 操作有关,在允许方法返回之前,必须在try-with-resources 语句中调用该操作。

我没有找到 JLS 的特定部分指定返回的情况。
所以你必须考虑到一般解释是适用的:

14.20.3. try-with-resources

...

如果所有资源初始化成功,try 块执行为 正常,然后是 try-with-resources 的所有非空资源 语句已关闭。

请注意,如果没有try-with-resources,您可能会编写以下代码:

try(Foo foo = new Foo()){
    return 1;
}
catch (RuntimeException e){
    //handle exception without throwing
}
return 2;

这样:

try{
    Foo foo = new Foo();
    foo.close(); // handled automatically by  try-with-resources 
    return 1;
}       
catch (RuntimeException e){
    //handle exception without throwing
}
return 2;

所以为什么1不能被返回应该是有道理的。
请注意,编译器由try-with-resources 生成的代码比我提供的伪等价代码更长、更复杂,因为抑制了异常。但这不是你的问题,所以让我赞成这个观点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-02
    • 2019-03-12
    • 1970-01-01
    • 2017-12-04
    • 2020-04-30
    • 1970-01-01
    • 2021-07-27
    • 2014-05-05
    相关资源
    最近更新 更多