Skip to main content

New/strange Java "try()” syntax?


While messing around with the custom formatting options in Eclipse, in one of the sample pieces of code, I saw code as follows:




/**
* 'try-with-resources'
*/
class Example {
void foo() {
try (FileReader reader1 = new FileReader("file1"); FileReader reader2 = new FileReader("file2")) {

}
}
}



I've never seen try used like this and I've been coding in Java for 9 years! Does any one know why you would do this? What is a possible use-case / benefit of doing this?



An other pieces of code I saw, I thought was a very useful shorthand so I'm sharing it here as well, it's pretty obvious what it does:




/**
* 'multi-catch'
*/
class Example {
void foo() {
try {
} catch (IllegalArgumentException | NullPointerException | ClassCastException e) {
e.printStackTrace();
}
}
}


Source: Tips4allCCNA FINAL EXAM

Comments

  1. It was added in Java 7. It's called the try-with-resources statement.

    /edit

    Might as well throw this in here too. You can use the try-with-resources statement to manage Locks if you use a wrapper class like this:

    public class CloseableLock implements Closeable {
    private final Lock lock;

    private CloseableLock(Lock l) {
    lock = l;
    }

    public void close() {
    lock.unlock();
    }

    public static CloseableLock lock(Lock l) {
    l.lock();
    return new CloseableLock(l);
    }
    }

    try(CloseableLock l = CloseableLock.lock(lock)) { // acquire the lock
    // do something
    } // release the lock


    However, since you have to declare a variable for every resource, the advantage of this is debatable.

    ReplyDelete
  2. This is Java 7's new try-with-resources statement: http://download.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html

    ReplyDelete
  3. Those are changes introduced in JDK7.

    First statement is a try-with-resources. I don't know exactly why they exist but exceptions are often caused by inputstreams etc, I guess it just improves readability. Edit: thanks to the other answerers, I read the javadoc and I now know that it will close all i/o streams that implement AutoCloseable, omitting the need for a finally block in a lot of situations

    Second is a multi-catch, which is really handy when you have different exceptions that you handle in exactly the same way.

    ReplyDelete
  4. It's called try-with-resource. It's a way so as to not have to clean after yourself as the language will do it for you.

    ReplyDelete
  5. That is called with a try with resources. in a try with resources, any kind of closable stream declared in the resources section will be closed after the try statement is done. So it pretty much is a

    try{
    InputStream is;
    //Stuff
    }finally{
    is.close()
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex