Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();         }     } } 
like image 814
Ali Avatar asked Apr 11 '12 23:04

Ali


People also ask

What is try syntax in Java?

The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

How does try finally work java?

What Is finally? finally defines a block of code we use along with the try keyword. It defines code that's always run after the try and any catch block, before the method is completed. The finally block executes regardless of whether an exception is thrown or caught.

What is try with resources in Java example?

The try -with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try -with-resources statement ensures that each resource is closed at the end of the statement.

Can we use try on a single statement in Java?

Java try blockIf an exception occurs at the particular statement in the try block, the rest of the block code will not execute. So, it is recommended not to keep the code in try block that will not throw an exception. Java try block must be followed by either catch or finally block.


2 Answers

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.

like image 100
Jeffrey Avatar answered Sep 20 '22 13:09

Jeffrey


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

like image 24
Michael Schmeißer Avatar answered Sep 20 '22 13:09

Michael Schmeißer