/home/wpollock1/public_html/AJava/TryWithResources.java
// Demo of Java 7 try-with-resources.
// Written 1/2012 by Wayne Pollock
// Adapted from an article on TheServerSide.com 12/29/2011,
// "Use try-with-resources: Language Enhancements for the Java 7 OCPJP Exam"
// by Sal Pece and Cameron McKenzie
public class TryWithResources
{
public static void main ( String [] args )
{
try ( OpenTheWindow win = new OpenTheWindow() )
{
System.out.println( "Window is open." );
}
catch ( Exception e ) {
System.out.println( "Exception caught" );
}
finally {
System.out.println( "In finally clause." );
}
}
}
class OpenTheWindow implements AutoCloseable
{
public OpenTheWindow ()
{
System.out.println( "The window is opening." );
}
public void close () throws Exception
{
System.out.println( "The window is closed." );
}
}
/******************************************
Output:
C:\Temp>java TryWithResources
The window is opening.
Window is open.
The window is closed.
In finally clause.
******************************************/