• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about finally and close()

 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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?
 
Paweł Michalak
Greenhorn
Posts: 1
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's too old got to do with it?
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic