99

As per as my knowledge we use try catch as follows:

try {
   //Some code that may generate exception
}
catch(Exception ex) {
}
   //handle exception
finally {
   //close any open resources etc.
}

But in a code I found following

try(
    ByteArrayOutputStream byteArrayStreamResponse  = new ByteArrayOutputStream();                   
    HSLFSlideShow   pptSlideShow = new HSLFSlideShow(
                                      new HSLFSlideShowImpl(
 Thread.currentThread().getContextClassLoader()
       .getResourceAsStream(Constants.PPT_TEMPLATE_FILE_NAME)
                                     ));
 ){
}
catch (Exception ex) {
       //handel exception
}
finally {
      //close any open resource
}

I am not able to understand why this parentheses () just after try.

What is the usage of it? Is it new in Java 1.7? What kind of syntax I can write there?

Please also refer me some API documents.

user207421
  • 305,947
  • 44
  • 307
  • 483
Partha Sarathi Ghosh
  • 10,936
  • 20
  • 59
  • 84
  • Possible duplicate of [Am I using the Java 7 try-with-resources correctly](http://stackoverflow.com/questions/17650970/am-i-using-the-java-7-try-with-resources-correctly) – Petter Friberg Jun 02 '16 at 12:06
  • You are asking what it is, I have linked to a question not only what it is, but also how to use it correctly. – Petter Friberg Jun 02 '16 at 12:07

1 Answers1

105

It is try with Resources syntax which is new in java 1.7. It is used to declare all resources which can be closed. Here is the link to official documentation. https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

static String readFirstLineFromFile(String path) throws IOException {
try (BufferedReader br =
               new BufferedReader(new FileReader(path))) {
    return br.readLine();
}
}

In this example, the resource declared in the try-with-resources statement is a BufferedReader. The declaration statement appears within parentheses immediately after the try keyword. The class BufferedReader, in Java SE 7 and later, implements the interface java.lang.AutoCloseable. Because the BufferedReader instance is declared in a try-with-resource statement, it will be closed regardless of whether the try statement completes normally or abruptly (as a result of the method BufferedReader.readLine throwing an IOException).

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • 8
    As per my understanding now after reading your referred documentation, It try to omits finally bock where we need to put another try to handle the exception to close the resource. Nice, some times I felt bad when I write another try catch inside finaly to handle the issue. – Partha Sarathi Ghosh Jun 02 '16 at 05:50
  • Would this work with database objects, PreparedStatement, ResultSet? – jpruiz114 Oct 11 '19 at 15:00
  • @jpruiz114 Yes. They will work with both. As that classes implements the `AutoClosable` interface. – sdsc81 Mar 16 '21 at 18:00
  • Another way to imagine: variable declared in try () which is an instance of AutoCloseable. – Macintosh Fan Apr 04 '22 at 01:35
  • 1
    For those familiar with Python, this seems like an equivalent of context managers (the `with ... as` clause). – Sam Chats Apr 18 '23 at 16:37