posted 18 years ago
I'm a big advocate of carefully logging any exception which you don't completely understand the cause of at the time you catch it. Which is to say, almost all exceptions - though there are a few situations where you know the cause and you don't care about the stack trace. (InterruptedException is a common example.)
But going back to the original code here, there are two significant problems. The really big one is: what about the very first catch block for SQLException e, on line 9? Something went wrong in the preceding SQL code... and then you do a rollback, and you close the connection, and probably nothing will go wrong there, and if it does there's not much you can do about it anyway. OK. But what about the original exception? Something went wrong, And after this code runs, THERE IS NO INDICATION ANYWHERE THAT A PROBLEM HAS OCCURRED. No error message. No log file. If someone eventually looks at the database, they may notice that some expected data is missing. Or not. But it's going to take some work for someone to figure out what went wrong here. With luck, that person will be you. Because if it's someone else, and they spend a long time tracking this down before they find this one bit of code here that has successfully hidden the evidence of the error, and they find out you are the author...
Well, that's the sort of thing that motivates hardworking programmers to lock their co-workers in empty rooms and force them to listen for hours to Barry Manilow recordings. Or other similar forms of torture.
Seriously, exceptions contain valuable, useful information. Typically including actual error messages in addition to the stack trace itself. This is really good stuff to have a record of when things go wrong. You never, ever want to just lose an exception just because you weren't sure what to do with it. That just makes it much harder to debug problems later. Using a logging framework is a good idea - but even if you don't have one set up yet, at the very least you can call e.printStackTrace(). That usually does a pretty good job of dumping all the info you need someplace it will probably be found. Much better than doing nothing.
In the above code, you weren't doing nothing, but you let confusion over what to do about the second and third catch blocks (lines 12 and 19) prevent you from logging the most important exception, the one on line 9. That's the one that would tell you what SQL error actually started whatever problem you may be having.
I mentioned two problems. The other one is, what if there's an error on line 2? You'll go to the catch block, and then the finally block... but the connection is null. So lines 11 and then 18 will both throw NullPointerException. Oops. There are two was to avoid this. One is to put "if (conn == null)" checks before the rollback() and the close(). The other, which I prefer, is to place try/catch/finally blocks carefully such that the rollback and close do not occur at all unless getConnection() succeeds:
Of course e.printStackTrace() can be replaced with proper logging - as long as you do something.
[ May 04, 2006: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister