Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception handling

 
Branko Paskutini
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, does anyone know good web resources (articles, web pages, whatever) where the "proper" Exception handling is explained, specifically when coding against database? Something like "best practices" in Exception handling.
I am writing server component of the application which is manipulating the db. Requests for db manipulation will come from the clients via Servlets, and my component will be sending them Responses.
I was only able to find introductions or EJB specific articles. What I am after is a real-life Exception handling policies for JDK and JDBC.
Regards, branko.
 
Anand Shivadas
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont any resources, but general rule of thumb:
1. Always catch SQLException (obviously) and rollback transaction
2. Always close resultset, statement and connection in finally block - use separate methods to close them
Ex:
finally {

closeResultset(rsValidate);
closeStatement(stmtValidate);
closeConnection( connValidate );
}
(even if closeResultset raise exception, closeStatement and closeConnection will be executed)

and closeResultset(Resultset rs) is defined as,
private static final void closeResultset( ResultSet rs ) {
try {
rs.close();
}
catch(Exception ex ) {
}
}
Correct me if I am wrong -
 
Janet Wilson
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Branko,
I wouldn't go so far as to say that the following is a "best practices" - but if you are looking for free information that is clear on Exception Handling you might want to check out:
Bradley Kjell's Beginning Java course - chapter 80. You'll want to look at Chapter 81, too.
This link at IBM goes into the finally keyword which may be of some help, too:
Using Finally to avoid resource links
Of course, there is always the Sun site which is pretty good in the area of JDBC. These few links might help you out:
JDBC 2.0 Fundamentals
Adv Tutorial on JDBC
JDBC API Tutorial and Reference
Good Luck!
Janet
[ November 12, 2002: Message edited by: Janet Wilson ]
[ November 12, 2002: Message edited by: Janet Wilson ]
 
Branko Paskutini
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Janet and Anand.
I didn't mean to scare anyone with my 'best practices' statement, I just thought that would better explain what I am after. Come to think of it, ANY practices would do.
I will be closing all the resources in the 'finally' block, but the difficult part for me is what to do within the 'catch', apart from logging it (will most likely use Log4J). I read about the exception chaining, seems like a good start.
http://www.javaworld.com/javaworld/jw-09-2001/jw-0914-exceptions-p2.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic