weixin_42315883 2009-06-17 12:02
浏览 349
已采纳

关于try catch的问题

昨天2个同事在数据库连接释放时,讨论一个问题,是关于try catch的,在这个问题上,我也吃不太准,在这里向各位讨教一下。
代码如下:
代码片段一:
Connection conn = null;
Statement st = null;
ResultSet rs = null;
...
...
...
finally {
try{
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}

代码片段二:
Connection conn = null;
Statement st = null;
ResultSet rs = null;
...
...
...
finally {
if (rs != null)
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}

if (st != null)
try{
    st.close();
}catch(SQLException e){
    e.printStackTrace();
}

if (conn != null)
try{
    conn.close();
}catch(SQLException e){
    e.printStackTrace();
}

}

在代码片段中大家可以清晰的看到在释放Connection Statement ResultSet3个资源时,片段一将释放代码放到一个try catch快中,而片段二是放到三个try catch快中。
两个同事的争议也在这里。
写片段二代码的同事,认为写在一个try catch快中,一旦前面的资源在释放抛出异常,后面的资源就不会再释放,这点可以肯定,同时他认为如果分成3个try catch快,一旦前面的资源抛出异常,后面的资源依然会继续释放资源。
写片段一代码的同时,认为写在一个try catch快中还是写在三个try catch快中,在前面的资源释放抛出异常后,程序就中断了,后面的资源都不会再释放了。

在这里我也有些困惑,请大家谈谈你们的看法,谢谢。
[b]问题补充:[/b]
写片段一的同事,是这样理解的,java代码是顺序执行的,一旦抛出异常将终止程序,所以他认为,及时写在3个块中,也还是一样的,在前面资源释放是出现了异常中断程序执行,后面的资源一样不会释放。
我不知道怎么去说服他,各位能不能给我写意见。

  • 写回答

2条回答

  • wanghaolovezlq 2009-06-17 12:34
    关注

    写在三个try catch快中的方式比较好

    看看spring的做法就知道
    [code="java"]
    org.springframework.jdbc.support.JdbcUtils
    /**
    * Close the given JDBC Connection and ignore any thrown exception.
    * This is useful for typical finally blocks in manual JDBC code.
    * @param con the JDBC Connection to close (may be null)
    */
    public static void closeConnection(Connection con) {
    if (con != null) {
    try {
    con.close();
    }
    catch (SQLException ex) {
    logger.debug("Could not close JDBC Connection", ex);
    }
    catch (Throwable ex) {
    // We don't trust the JDBC driver: It might throw RuntimeException or Error.
    logger.debug("Unexpected exception on closing JDBC Connection", ex);
    }
    }
    }

    /**
     * Close the given JDBC Statement and ignore any thrown exception.
     * This is useful for typical finally blocks in manual JDBC code.
     * @param stmt the JDBC Statement to close (may be <code>null</code>)
     */
    public static void closeStatement(Statement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
            }
            catch (SQLException ex) {
                logger.debug("Could not close JDBC Statement", ex);
            }
            catch (Throwable ex) {
                // We don't trust the JDBC driver: It might throw RuntimeException or Error.
                logger.debug("Unexpected exception on closing JDBC Statement", ex);
            }
        }
    }
    
    /**
     * Close the given JDBC ResultSet and ignore any thrown exception.
     * This is useful for typical finally blocks in manual JDBC code.
     * @param rs the JDBC ResultSet to close (may be <code>null</code>)
     */
    public static void closeResultSet(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            }
            catch (SQLException ex) {
                logger.debug("Could not close JDBC ResultSet", ex);
            }
            catch (Throwable ex) {
                // We don't trust the JDBC driver: It might throw RuntimeException or Error.
                logger.debug("Unexpected exception on closing JDBC ResultSet", ex);
            }
        }
    }
    

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮