• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Do the try-with-resources implicit finally block and the explicit finally block behave differently?

 
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reading that the try-with-resources implicit finally block is called before any catch blocks or even the finally block coded by us.
As I understand, the finally block coded by us is called after the catch blocks, and before a return of control back out of the method.
Is the only purpose of this implicit finally block to close the open resources?
 
Saloon Keeper
Posts: 15731
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct.

The reason that it works this way is that it enables you to catch any exception that might arise from the closing of a resource:

With a regular try-finally statement, you'd have to wrap it in a second try-catch statement in order to catch the exception that results from closing the resource.
 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Correct.

The reason that it works this way is that it enables you to catch any exception that might arise from the closing of a resource:



Thanks. I have a further question.
Are you saying that if one of the resources throws a checked exception (say, FileNotFoundException) then we do not have to catch it explicitly because the implicit finally block will do it for us?
I understand that in the older explicit try-finally, we would have to explicitly have a catch for this checked exception.
 
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think Stephan said that at all.  He's just pointing out that with try-with-resources you don't need a second try/catch to handle an exception during close() - it's all covered by the single try, if you add a catch - such as the one Stephan showed.

The implicit finally block (or any finally block) doesn't actually catch an exception.  If an exception is thrown, then the finally block will execute, putting a "pause" on the exception while finally executes.  But then when finally is done, it will continue throwing the exception as before.  Unless there is also a catch block for the exception.

For a checked exception, you do still need to either catch the exception, or declare that it can be thrown from that method.
 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:
The implicit finally block (or any finally block) doesn't actually catch an exception.  If an exception is thrown, then the finally block will execute, putting a "pause" on the exception while finally executes.  But then when finally is done, it will continue throwing the exception as before.  Unless there is also a catch block for the exception.


Thanks. I understood it now.
So again, the difference is the explicit finally runs after the catch block, but the implicit finally runs before the catch block?
 
Marshal
Posts: 79971
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will find the gory details in the JLS; look for the yellowish rectangle following this:-

The meaning of a basic try-with-resources statement of the form:

try ({VariableModifier} R Identifier = Expression ...)
   Block



is given by the following translation to a local variable declaration and a try-catch-finally statement:

I think that will tell you what you want to know.

I can't quite reproduce the formatting in the JLS.
 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic