尝试使用Java 1.6中的资源

sch*_*her 10 java try-catch try-with-resources

我有以下代码:

    public class Main {

        public static void main(String[] args) throws SQLException {

            try (
                    Connection conn = DBUtil.getConnection(DBType.HSQLDB);
                    Statement stmt = conn.createStatement(
                            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                    ResultSet rs = stmt.executeQuery("SELECT * FROM tours");
                    ) {

            DBUtil.getConnection();

            } catch (SQLException e) {
                DBUtil.processException(e);
            } 

        }

    }
Run Code Online (Sandbox Code Playgroud)

我使用此代码从数据库中获取数据.我的问题是我不允许使用Java 1.7编译器并且必须使用1.6.如何将try-with-resources-code转换为与1.6编译器一起使用?在这个特殊的尝试块中究竟发生了什么?

Ord*_*ous 11

Oracle解释了try-with-resources如何在这里工作

TL;它的DR是:
在Java 1.6中没有简单的方法.问题是Exception中没有Suppressed字段.您可以忽略它并硬编码当尝试AND关闭抛出不同异常时发生的事情,或者创建具有抑制字段的自己的Exception子层次结构.

在第二种情况下,上面的链接提供了正确的方法:

   AutoClose autoClose = new AutoClose();
   MyException myException = null;
   try {
       autoClose.work();
   } catch (MyException e) {
       myException = e;
       throw e;
   } finally {
       if (myException != null) {
           try {
               autoClose.close();
           } catch (Throwable t) {
               myException.addSuppressed(t);
           }
       } else {
           autoClose.close();
       }
   }  
Run Code Online (Sandbox Code Playgroud)

相当于

try (AutoClose autoClose = new AutoClose()) {
    autoClose.work();
}
Run Code Online (Sandbox Code Playgroud)

如果你想让它更容易并且不创建大量新的Exception类,你将不得不决定在finally(t或e)中的catch子句中抛出什么.

PS.在上面的链接中还讨论了在try中处理多个变量声明.而正确执行它所需的代码量是惊人的.大多数人通过不处理finally块中的异常并使用nullchecks来获取Java 1.6中的快捷方式.


小智 0

像这样做:

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try {
    conn = DBUtil.getConnection(DBType.HSQLDB);
    stmt = conn.createStatement(
    ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    rs = stmt.executeQuery("SELECT * FROM tours");
} catch (SQLException e) {
    DBUtil.processException(e);
} finally {
    if(conn != null) {
        conn.close();
    }
    if(stmt != null) {
        stmt.close();
    }
    if(rs != null) {
        rs.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • -1 这不等于 try-with-resources。特别是在异常处理方面。 (2认同)