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条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型