This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Question about finally and close() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about finally and close()" Watch "Question about finally and close()" New topic
Author

Question about finally and close()

Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 142

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?


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Paweł Michalak
Greenhorn

Joined: Mar 16, 2011
Posts: 1

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

Joined: Oct 13, 2005
Posts: 37950
    
  22
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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

Joined: Oct 13, 2005
Posts: 37950
    
  22
What's too old got to do with it?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about finally and close()
 
Similar Threads
try without catch
file download method
doConfigure method in Configuration class
problem with IO Exception
Try catch confusion