Julho 06
Prof(s). Eduardo Bezerra & Ismael H. F. Santos                                                2
Best Practices for Unsing Exceptions
1.Always clean up after yourself (cont.)
n
nclass DBUtil{
n  public static void closeConnection (Connection conn) {
n    try{
n      conn.close();
n    } catch(SQLException ex) {
n      logger.error("Cannot close connection");
n      throw new RuntimeException(ex);
n    }
n  }
n}
nDBUtil is a utility class that closes the Connection. The important point is the use of finally block, which executes whether or not an exception is caught. In this example, the finally closes the connection and throws a RuntimeException if there is problem with closing the connection.