opi*_*ike 2 java exception-handling try-catch-finally
是否有一种内置方法可以在finally块中确定您是否刚刚从catch块中出来?我知道这可以通过下面的变量轻松完成,但我很好奇是否有标准的内置方式.
boolean bException = false;
try
{
dbf = new DBFunctions();
dbf.db_run_query(query2);
dbf.rs.next();
nMonth = dbf.rs.getInt("calquarter") * 3;
nYear = dbf.rs.getInt("calyear");
}
catch (SQLException sqle)
{
out.println(sqle.toString());
bException = true;
}
finally
{
dbf.closeConnection();
if (bException == true)
return;
}
Run Code Online (Sandbox Code Playgroud)
更新:以下是closeConnection()方法的内容,它只是试图关闭所有数据库对象:
public void closeConnection()
{
if (rs != null)
{
try { rs.close(); } catch (SQLException e) { ; }
rs = null;
}
if (stmt != null)
{
try { stmt.close(); } catch (SQLException e) { ; }
stmt = null;
}
if (con != null)
{
try { con.close(); } catch (SQLException e) { ; }
con = null;
}
}
Run Code Online (Sandbox Code Playgroud)
不是我所知道的.在这种情况下,然而,你可能会简单地把更好return的内部catch块.该finally块仍将运行.