关于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个块中,也还是一样的,在前面资源释放是出现了异常中断程序执行,后面的资源一样不会释放。
我不知道怎么去说服他,各位能不能给我写意见。
[b]问题补充:[/b]
谢谢yourgame,我知道怎么说服他了,哈哈。

先扁了再说.死硬派

try块中到代码发生异常后,会先去catch,然后去finally,然后继续走下去.除非不是异常,是Error那就是致命到拉.trycatch trycatch 就是可以捕获到错误嘛,如果不继续走了.补他干鸟.之所以能够处理,也就是还可以继续走下去撒

一般是按片段二写的

try中出现异常,一定会进入相应的catch中,异常语句之后的try中的语句不会执行,所以一般按照片段2写,

假如在 rs.close(); 时异常,一定会进入对应的catch,
如果是片段一
[code="java"]if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}[/code]

这2句就不会再执行了。

如果是片段2,
rs.close(); 异常后就没有其他代码了,会在catch之后进入catch的下一个语句:新的try catch

所以片段2是能保证资源合理释放的,而片段1会导致资源泄漏

写在三个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]

很明显嘛,分开线是规范的.安全的.不要为了节省代码而不看性能

把代码一到同事叫过来扁一顿 [color=red][size=x-large]想不想[/size][/color]