• 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

Question about finally and close()

 
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey!

I have the following block of code:



Now, Eclipse tells me that I have to surround stream.close() with a try and catch-block - doesn't this defeat the purpose of the finally-block when I need to have another try/catch-block inside it?
 
Greenhorn
Posts: 1
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the code in finally, behaves as any of the code in Java, so it must declare or handle the exception that is thrown by the called code. So You can surround stream.close() with try and catch, or throw the exception in the declaration of the method that is calling this code.
 
Marshal
Posts: 79180
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 Paweł Michalak. You are correct about the Exception from the finally block.

What you want is this sort of thing:That ensures that if you can actually open your reader, it is closed, because the finally block is always executed whether or not the try suffers an Exception. Since the close() method declares a checked Exception, having it inside the outer try ensures that Exception is handled too.

I have added lots of comments to try and make the control flow easier to see. Probably more than you would want in real life. The format for file streams, data input streams, writers, etc, is very similar.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ugh. That code looks horrible, because of the null checks and all. And you don't even need those:
I do this all the time - try+finally to make sure the resource is closed, wrapped inside a try+catch to catch any exceptions occurred inside the block.

There is one issue with this, both in your code and mine. If inRead.close() throws an IOException it will overwrite any IOException thrown by the remainder of the code.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote: . . . because of the null checks and all. And you don't even need those:. . . .

Never realised that. Thank you.

. .. If inRead.close() throws an IOException it will overwrite any IOException thrown by the remainder of the code.

You can't win them all.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Rob Spoor wrote: . . . because of the null checks and all. And you don't even need those:. . . .

Never realised that. Thank you.


You're never too old to learn something new
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's too old got to do with it?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing, I was just kidding, and definitely not referring to your age (which I actually don't even know; all I know is you're older than me, but 35 is already older than me). Perhaps that statement would be better if I'd replace "old" with "experienced".
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic