3 ms·
I'd add "with" statements. No more: Closeable dbConnection; try { dbConnection = CloseableFactory.getConnection(); doWork(dbConnection); } catch
by frio 15y ago
I'd add "with" statements. No more:
Closeable dbConnection;
try {
dbConnection = CloseableFactory.getConnection();
doWork(dbConnection);
} catch (CloseableException ex) {
// log it or whatever
} finally {
// the really annoying, stupid, crufty boilerplate bullshit
try {
if (dbConnection != null)
dbConnection.close();
} catch (CloseableException ex) {
// it's probably already closed
}
}
Instead, it'd just be
with (Closeable dbConn = CloseableFactory.getConnection()) {
doWork(dbConn);
}
(Assuming the "with" statement eats errors relating to the instantiation/closing of the Closeable, anyway). Bliss. And I know there's stuff like Google Guava's Closeables.closeQuietly(conn);, but still, a with statement would be awesome.
- benjiweber 15y agoIf only it already existed in Java http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html http://docs.oracle.com/javase/tutorial/essential/exceptions/...
- frio 15y agoWow. I hadn't looked at Java 7 much; I've been under the assumption adoption would be reasonably slow so haven't really dug into it yet. Cool!