Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why write Try-With-Resources without Catch or Finally?

Why write Try without a Catch or Finally as in the following example?

protected void processRequest(HttpServletRequest request, HttpServletResponse response)         throws ServletException, IOException {     response.setContentType("text/html;charset=UTF-8");     try (PrintWriter out = response.getWriter()) {         /* TODO output your page here. You may use following sample code. */         out.println("<!DOCTYPE html>");         out.println("<html>");         out.println("<head>");         out.println("<title>Servlet tryse</title>");                     out.println("</head>");         out.println("<body>");         out.println("<h1>Servlet tryse at " + request.getContextPath() + "</h1>");         out.println("</body>");         out.println("</html>");     }  } 
like image 204
Umair Hashmi Avatar asked Oct 14 '14 09:10

Umair Hashmi


People also ask

Does try-with-resources need catch or finally?

Note: A try -with-resources statement can have catch and finally blocks just like an ordinary try statement. In a try -with-resources statement, any catch or finally block is run after the resources declared have been closed.

What is the advantage of using try-with-resources statement?

Benefits of using try-with-resources: More readable code and easy to write. Automatic resource management. Number of lines of code is reduced.

Is catch block required in try-with-resources?

You can add a catch block to a try-with-resources block just like you can to a standard try block. If an exception is thrown from within the try block of a try-with-resources block, the catch block will catch it, just like it would when used with a standard try construct.

What are the benefits of try with resource in java give code lines to show its syntax?

For try-with-resources, if an exception is thrown in a try block and in a try-with-resources statement, then the method returns the exception thrown in the try block. The exceptions thrown by try-with-resources are suppressed, i.e. we can say that try-with-resources block throws suppressed exceptions.


2 Answers

As explained above this is a feature in Java 7 and beyond. try with resources allows to skip writing the finally and closes all the resources being used in try-block itself. As stated in Docs

Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

See this code example

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

In this example the resource is BufferReader object as the class implements the interface java.lang.AutoCloseable and it will be closed whether the try block executes successfully or not which means that you won't have to write br.close() explicitly.

Another important thing to notice here is that if you are writing the finally block yourself and both your try and finally block throw exception then the exception from try block is supressed.

While on the other hand if you are using try-with-resources statement and exception is thrown by both try block and try-with-resources statement then in this case the exception from try-with-resources statement is suppressed.

As the @Aaron has answered already above I just tried to explain you. Hope it helps.

Source: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

like image 59
Syed Ali Taqi Avatar answered Oct 17 '22 02:10

Syed Ali Taqi


This is a new feature in Java 7 and beyond. Without this, you'd need a finally block which closes the resource PrintWriter out. So the code above is equivalent to:

PrintWriter out = null; try {     PrintWriter out = ... } finally {     if(null != out) {         try {             out.close();         } catch(Exception e) {} // silently ignore!     } } 

See The try-with-resources Statement

like image 21
Aaron Digulla Avatar answered Oct 17 '22 00:10

Aaron Digulla