Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Round brackets / parentheses () in try catch in Java

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.

like image 757
Partha Sarathi Ghosh Avatar asked Jun 02 '16 05:06

Partha Sarathi Ghosh


People also ask

What is the use of round brackets in Java?

In Java, brackets are used for the following purposes: Round brackets () Arguments of methods are placed between round brackets. Furthermore, round brackets are used in mathematical formulas to specify priorities of operations, and in control structures such as for-loops and if-clauses.

How will you come out of try {} block?

The proper way to do it is probably to break down the method by putting the try-catch block in a separate method, and use a return statement: public void someMethod() { try { ... if (condition) return; ... } catch (SomeException e) { ... } }

What is the use of try ___ catch block?

Java try block is used to enclose the code that might throw an exception. It must be used within the method. If 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.

What is the syntax of try-catch?

The “try… It works like this: First, the code in try {...} is executed. If there were no errors, then catch (err) is ignored: the execution reaches the end of try and goes on, skipping catch . If an error occurs, then the try execution is stopped, and control flows to the beginning of catch (err) .


1 Answers

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

like image 169
Prasad Kharkar Avatar answered Sep 23 '22 18:09

Prasad Kharkar