• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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
 
Saloon Keeper
Posts: 9143
172
  • 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
Rancher
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
Saloon Keeper
Posts: 9143
172
  • 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: 3601
39
  • 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
Rancher
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: 21424
94
Chrome Eclipse IDE Java 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
Saloon Keeper
Posts: 9143
172
  • 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: 59769
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!