I look at Java code and it horrifies me. Java is great if your resource is memory. It turns into a horrible unsafe mess the moment your resources are not memory. //C++ RAII goodness. void foo() { conn.open(); file.open(); printer.activate(); //do stuff } //Java's lack of RAII is disturbing void foo() { try{ conn.open(); file.open(); printer.activate(); //do stuff } catch(Exception e) { } finally{ try { conn.close();…
void foo() {
try (open file or resource here) {
// do stuff
}
// after try block, resource will close automatically
}
This works with non-memory resources such as files, streams, sockets, and database connections.