File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Exception handling" Watch "Exception handling" New topic

Exception handling

Branko Paskutini

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

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
finally {

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 {
catch(Exception ex ) {
Correct me if I am wrong -
Janet Wilson
Ranch Hand

Joined: Jul 16, 2002
Posts: 98
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!
[ November 12, 2002: Message edited by: Janet Wilson ]
[ November 12, 2002: Message edited by: Janet Wilson ]
Branko Paskutini

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.
I agree. Here's the link:
subject: Exception handling
It's not a secret anymore!