This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes try / catch / finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "try / catch / finally" Watch "try / catch / finally" New topic
Author

try / catch / finally

Ernie Mcracken
Ranch Hand

Joined: Feb 13, 2011
Posts: 33

Im trying to get my head around try / catch / finally blocks. I want to be able to use finally to close an input stream.. but I can't figure out the code i need to use. Heres what I have. How do I organise my code to get it right?



At compile time I get cannot find symbol: variale fr, I guess this means that I cannot access the fis object from the finally statement, but I don't know how to organise the code so that I can.

You're on a gravy train with biscuit wheels Roy.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4246
    
    7

You need to declare fis outside the block, so it can be accessed in both the try and finally blocks. Like this:
Ernie Mcracken
Ranch Hand

Joined: Feb 13, 2011
Posts: 33

That simple! thanks very much.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: try / catch / finally
 
Similar Threads
checked exception
try,catch and finally blocks
Finally {IOException}
Can a static final constant throw an exception?
Directory / File not found