A tricky try-with-resources question to take you into the New Year

Discussions

News: A tricky try-with-resources question to take you into the New Year

  1. 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

    Threaded Messages (4)

  2. duplicate directory auto remover[ Go to top ]

    In case there is a need to auto remove huge number of duplicate  directories / files : http://sourceforge.net/projects/off-dup-finder . (if suspecting malware in this program(done in gcj-jdk), run in offline mode: take "ls -laR" output of any directory on ubuntu linux and give to this program to generate duplicate compare & removal script). ( auto remove/merge programs is  open source).

    Hope similar features is created in some free source- content management systems.. (weighted similarity between tree structures, Subset  directory removal, cyclic conflict resolution etc..)

     

  3. Tricky?[ Go to top ]

    I don't see the tricky part, it does what is expected.

    The exception handling in the example code is horrible and should be rewritten, even for an example: catch-all, no real handling which lets the program continue after the catch block, ...

  4. Tricky?[ Go to top ]

    I don't see the tricky part, it does what is expected.

    The exception handling in the example code is horrible and should be rewritten, even for an example: catch-all, no real handling which lets the program continue after the catch block, ...

     

  5. 404: Page not found[ Go to top ]

    JFYI, this is the title that my browser shows for this page. The content is all there, but window title is confusing.