【问题标题】:try-with-resources statement in JavaJava 中的 try-with-resources 语句
【发布时间】:2014-05-16 18:11:52
【问题描述】:

在这个 Java 程序示例中:

package test;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;

public class Test
{
    private static void example(){
        String url = "jdbc:oracle:thin:@//localhost:7856/xe";
        String user = "user";
        String password = "pass";
        try(Connection con = DriverManager.getConnection(url, user, password);
            Statement stmt = con.createStatement()){
            throw new OutOfMemoryError("Error");
        }catch (SQLException e){
            System.err.println("SQLException");
        }
    }

    public static void main(String [] args){
        try{
            example();
        }catch (OutOfMemoryError e){
            System.err.println("OutOfMemoryError");
        }
        // Rest of code here...
    }
}

当在静态方法 example() 的主体中抛出 OutOfMemoryError 错误时,尽管没有任何“catch”,但在终止静态方法 example() 之前连接“con”和语句“stmt”是否自动关闭捕获这些错误,因此在 main() 的其余代码中确定这两个对象已关闭?

谢谢。

【问题讨论】:

    标签: java try-with-resources


    【解决方案1】:

    是的; try-with-resources 构造总是关闭资源,即使它是未经检查的可抛出对象(如 OutOfMemoryError)。

    这是在JLS 14.20.3 中指定的,它以一个非常通用的声明开始,即资源“自动关闭”,但随后进入了资源关闭时间的各种示例。基本上,任何非null 资源总是 关闭,就好像关闭是在仅为该资源创建的try-finallyfinally 子句中一样。即使try 中有多个资源也是如此,就像您的情况一样(“关闭一个资源的异常不会阻止关闭其他资源”)。

    简单的类来演示它:

    public class Twr {
      private static class TwrCloseable implements AutoCloseable {
        private final String id;
    
        TwrCloseable(String id) {
          this.id = id;
        }
    
        @Override
        public void close() {
          System.out.println("closing " + id);
        }
      }
    
      public static void main(String[] args) {
        try (TwrCloseable closeable1 = new TwrCloseable("first");
             TwrCloseable closeable2 = new TwrCloseable("second")) {
          throw new OutOfMemoryError();
        }
      }
    }
    

    输出:

    closing second
    closing first
    Exception in thread "main" java.lang.OutOfMemoryError
        at Twr.main(Twr.java:19)
    

    请注意,它们以相反的顺序关闭; “second”在“first”之前关闭。在您的示例中,这意味着 StatementConnection 之前关闭,这正是您想要的。

    【讨论】:

      猜你喜欢
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-18
      • 2012-03-04
      • 2015-04-26
      • 2013-07-21
      • 1970-01-01
      相关资源
      最近更新 更多