It's not a secret anymore!*
The moose likes JDBC and the fly likes Exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Exception handling" Watch "Exception handling" New topic
Author

Exception handling

Branko Paskutini
Greenhorn

Joined: Apr 18, 2002
Posts: 25
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

Joined: Oct 17, 2000
Posts: 22
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

Joined: Jul 16, 2002
Posts: 98
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

Joined: Apr 18, 2002
Posts: 25
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception handling