• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

benefit of try with resources  RSS feed

 
Greenhorn
Posts: 2
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
i want to ask, What is the benefit to use "try with resources" VS use a method close?

Thank you
 
Bartender
Posts: 9486
184
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It uses a lot less boilerplate code and it reduces the chance that the developer will forget to close their resources properly. Compare the code that you can write with try-with-resources against what you have to write when you don't use try-with-resources:



As you can see, another nice benefit is that you reduce the scope of your variables. In the first example, reader is declared only for the try-statement. In the second example, reader is declared for the entire block of code.
 
Author
Posts: 160
31
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The benefit is that you don't have to call the close method(s). If you know all the intricacies of calling close, it only saves you some typing. If you don't know how to get sure that all closeable resources will be correctly closed, it might avoid much bigger problems.

One additional benefit is that you don't have to declare the resources out of the try .. catch block (where they should be declared as null. For example:



instead of



Disclaimer: I know this code is wrong. It is this kind of error that you avoid when  using try with resources
 
Luis González
Greenhorn
Posts: 2
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the answers , happy valentine's day
 
Stephan van Hulst
Bartender
Posts: 9486
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a small note about your code Pierre-Yves: You should NEVER let any exception propagate out of a finally-block, because you effectively overwrite any useful information returned from the try-block.

Let's say the try-block returned successfully: Do we really want to overwrite that result just because the connection didn't close normally (presumably because it was already closed abnormally)? And if the try-block threw an exception, isn't that exception much more interesting to know about than any exceptions that might occur in the finally-block?
 
Rancher
Posts: 3737
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it's missing the null check.


It's a lot of boiler plate code.
 
Pierre-Yves Saumont
Author
Posts: 160
31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously, you did not read the small print:

Pierre-Yves Saumont wrote:Disclaimer: I know this code is wrong. It is this kind of error that you avoid when  using try with resources



 
Sheriff
Posts: 21463
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Let's say the try-block returned successfully: Do we really want to overwrite that result just because the connection didn't close normally (presumably because it was already closed abnormally)?


That's what try-with-resources does as well. If the close() method throws an exception, any successful result from the try block is discarded. Only exceptions are not overwritten, but instead have the close() exception added as a suppressed warning.
 
Stephan van Hulst
Bartender
Posts: 9486
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My bad. In that case, the simple try-with-resources I posted earlier is actually equivalent to:

That's a lot of boilerplate!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main advantage is that you don't have to perform closing activity in finally block.
 
Marshal
Posts: 61690
192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!