|
Re: Sources of Java Errors
|
Posted: May 27, 2008 11:51 AM
|
|
For multiple closes in the finally block,
One option is to use org.apache.commons.io.IOUtils.closeQuietly(x).
It checks for x==null, and eats any exception, which makes sure that everything will get closed (if possible).
Another idea (haven't see or testing this in practice) is for the wrapper class to collect a List of java.io.Closeable, and at the end to go (backwards) through this List closing things. But not all the JDBC still implements Closeable, only the java.io stuff does.
The tricky issue is what to do if close() throws an Exception. Apache ignores it, which, IMO, is about as good as you'll get. If the preceeding IO failed, you definitely want to ignore the close() exception, cause it's semi-expected, and you want to throw the "real exception", which is something in the preceeding IO. If it's just the close() that failed, ???. I've NEVER seen this happen, and whatcha going to do about it anyway? I wrote my own IOUtils-like class (cleverly called Finally, it was a ton of methods to call in a Finally block) that returned any Exception, so the caller had the option to do something. But in practice I never checked the result, or at most just logged it and ignored it, so I started using the more standard Apache stuff.
|
|