• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

benefit of try with resources

 
Greenhorn
Posts: 2
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15491
363
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 161
31
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the answers , happy valentine's day
 
Stephan van Hulst
Saloon Keeper
Posts: 15491
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 161
31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15491
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main advantage is that you don't have to perform closing activity in finally block.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic