Try-with-resources equivalent in Java 1.6

淺唱寂寞╮ 提交于 2019-11-27 05:27:33

Oracle explains how try-with-resources works here

The TL;DR of it is:
There is no simple way of doing this in Java 1.6. The problem is the absence of the Suppressed field in Exception. You can either ignore that and hardcode what happens when both try AND close throw different exceptions, or create your own Exception sub-hierarchy that has the suppressed field.

In the second case, the link above gives the proper way of doing it:

   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();
       }
   }  

is equivalent to

try (AutoClose autoClose = new AutoClose()) {
    autoClose.work();
}

In case you want to make it easier and not create a whole lot of new Exception classes, you will have to decide what to throw in the catch clause inside the finally (t or e).

PS. Dealing with multiple variable declaration in the try is also discussed in the link above. And the amount of code that you need to do it properly is staggering. Most people take shortcuts in Java 1.6 by not coping with exceptions in the finally block and using nullchecks.

Do it like this:

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();
    }
}

I would advise usage of apache's commons-dbutils library which have class DBUtils with close and closeQuietly methods. The code would look like this:

import org.apache.commons.dbutils.DBUtils;
...
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try {
    conn = myOwnUtil.getConnection();
    stmt = conn.createStatement();
    rs = stmt.executeQuery( "SELECT * FROM table" ); // or any other custom query
} catch ( SQLException e ) {
    <<handle exception here>>;
} finally {
    DBUtils.closeQuietly( conn );
    DBUtils.closeQuietly( stmt );
    DBUtils.closeQuietly( rs );
    // or simply use DBUtils.close( conn, stmt, rs );
}

Note that closeQuietly will throw no exceptions, while close might cast SQLException, so adapt the code to your own use case.

If you want to close streams than you can use apache's commons-io with IOUtils class which also have close and closeQuietly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!