Min*_*ang 1 java exception-handling
最近,我一直面临着在JDBC主题中抛出异常的问题.
我使用2个同步方法同步Connection对象:getConnection()和releaseConnection().然后另一个从数据库中删除行的方法如下:
public void removeItem(int itemID) throws ItemNotFound {
PreparedStatement ps = null;
String query = "DELETE * FROM Student.Book WHERE id = ?";
getConnection();
try {
ps = con.prepareStatement(query);
ps.setInt(1, bookID);
ps.executeUpdate();
} catch (SQLException sqle) {
this.close(null, null, ps); // method to close PreparedStatement and ResultSet
releaseConnection();
throw new BookNotFoundException("Book not found!");
} finally {
this.close(null, null, ps);
releaseConnection();
}
}
Run Code Online (Sandbox Code Playgroud)
如果没有异常发生,一切都很好.如果发生异常,在releaseConnection()方法之后的catch块中,抛出新的BookNotFoundException("找不到书!")挂起!! 如果我评论releaseConnection()方法然后它正常抛出?
归档时间: |
|
查看次数: |
720 次 |
最近记录: |