If you've been keeping up with the latest langauge enhancements in Java 7, then you'll have no problem at all figuring what the output of the code below will be when it is compiled into bytecode and run on the JVM.
For those of you who haven't polished up their Java 7 skills, especially with regards to the old try-with-resources enhancement, you can get updated quickly by reading this quick little tutorial on the subject:
Java 7 Language Enhancements: try-with-resources and the Closeable interface
So, what's the output of the following chunk of code? The answer is provided in detail here:
A tricky try-with-resources question
/************************************/
public class TryWithResources {
public static void main(String[] args) throws Exception {
try (OpenDoor door = new OpenDoor();OpenWindow window = new OpenWindow() ){
door.swing();
window.crank();
}
catch(Exception e) {System.out.println("Is there a draft?");}
finally {System.out.println("I'm putting a sweater on, regardless.");}
}
}
class OpenDoor implements AutoCloseable {
public OpenDoor() { System.out.println("The door is open."); }
public void swing() throws Exception {
System.out.println("The door is becoming unhinged.");
throw new Exception();
}
public void close() throws Exception {
System.out.println("The door is closed.");
}
}
class OpenWindow implements AutoCloseable {
public OpenWindow() { System.out.println("The window is open."); }
public void crank() throws Exception {
System.out.println("The window is overcranked!");
throw new Exception();
}
public void close() throws Exception{
System.out.println("The window is closed.");
}
}
/************************************/
Again, a fairly thorough discussion is provided here:A tricky try-with-resources question
You can also read up on the following tutorials from TheServerSide about other Java 7 enhancements:
Switch statements with Strings
Java 7 and binary notation
Numeric literals with underscores
Changes with the Java7 OCPJP Certification