OCPJP Java 7 Mock Certification Exam: A Tricky Question about ARM and Try-with-Resources

The Oracle Certified Professional Java Programmer (OCPJP) exam requires you to know the ins and outs of the new language features introduced in Java 7. This tricky question tests you on your knowledge of try-with-resources and AutoCloseable objects.

Take a quick look at the following piece of code, all of which would be compiled in a single file named TryWithResources.java. What is the final output when the code runs?

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.");
  }
}

Thinking through the logic, here is what happens: A door is opened and a window is opened, or at least, instances of the corresponding objects are created within the resource declaration section of the try block. 

    try (OpenDoor door = new OpenDoor();
     OpenWindow window = new OpenWindow() ) {

Then, the door is swung off it's hinge, and an exception is thrown. Because of the exception being thrown, the window never gets cranked. :(

  public void swing() throws Exception {
       System.out.println("The door is becoming unhinged.");
       throw new Exception();
  }

Before any exception handling code is executed, the resources opened in the resource declaration block are closed. This is a requirement of the automatic resource management (ARM) feature introduced in Java 7. However, this all happens implicitly, so we never actually see the call to the method close in our code.

Then, the exception handling occurs: first the catch block which handles the exception runs, and then the finally block, which always runs regardless of whether an exception is thrown in the body of the try block, is executed.

    catch(Exception e) {System.out.println("Is there a draft?");}
    finally {System.out.println("I'm putting a sweater on, regardless.");}

In the end, we see the following output:

C:\_jdk1.7\bin>java TryWithResources
The door is open.
The window is open.
The door is becoming unhinged.
The window is closed.
The door is closed.
Is there a draft?
I'm putting a sweater on, regardless.

 

 

Learning Resources for the Java and Java 7 Certification
 

OCP Java SE 6 Programmer Practice Exams (Exam 310-065) (Certification Press)
OCP Java SE 7 Programmer Study Guide (Certification Press)
SCJP Sun Certified Programmer for Java 6 Exam 310-065
A Programmer's Guide to Java SCJP Certification: A Comprehensive Primer (3rd Edition)
SCJA Sun Certified Java Associate Study Guide for Test CX-310-019, 2nd Edition

 

Check out these other tutorials from TheServerSide's Sal Pece and Cameron McKenzie covering the new Java 7 features:

New Java 7 Features: Binary Notation and Literal Variable Initialization
New Java 7 Features: Numeric Underscores with Literals Tutorial
New Java 7 Features: Using String in the Switch Statement Tutorial
New Java 7 Features: The Try-with-resources Language Enhancement Tutorial 
New Java 7 Features: Automatic Resource Management (ARM) and the AutoCloseable Interfact Tutorial
New Java 7 Features: Suppressed Exceptions and Try-with-resources Tutorial
Java 7 Mock Certification Exam: A Tricky OCPJP Question about ARM and Try-with-Resources
OCAJP Exam to Debuts in March 2010. OCPJP Released in June?
OCPJP & OCAJP Java 7 Exams: Oracle Drops the Training Requirement
OCAJP and OCPJP Changes for Java 7:  New Objectives, a Format Change and a Price Hike

 

This was last published in December 2011

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchCloudApplications

SearchSOA

SearchSoftwareQuality

SearchFinancialApplications

SearchSAP

SearchManufacturingERP

Close