This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

close a FileReader in the finally clause  RSS feed

 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I'm writing this simple method wich read the chars from a txt file and add it to a StringBuffer.

In anycase i'd like to close the stream putting in.close() in the finally clause, but I can't compile as it tells that the IO exception relative to the in.close() must be caught. But for me it wouldn't make any sense write a try catch statement into the finally. Any suggestions?

 
Jeremy Tartaglia
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, without modifications, this code will probably not compile. You're only returning a value in one case: the successful case. You need to return something in the failure case also. My recommendation is to make a faux finally clause.



It may not be elegant, but it works, and it's not even that difficult to read.
 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you're saing that I should always put the return statement just before the end of my methods as at least I'll get a null value if any errors incur.
Is that correct?
 
Jeremy Tartaglia
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It never hurts, and that's my first order of business in writing a function, but just follow the flow of logic. If there's some combination of events which could occur that would return nothing (not even null), then the Java compiler will pick that up too, and refuse to compile the program. And if you save your return statement for the very end of your procedure, you'll always know where to look. Of course, it's not always possible, so use your best judgement.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another try catch block inside the finally looks awkward but it's what's called for. If it really bothers you move it to another method.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way the taught the same idiom in the course I took was to use a nested a try/ finally block for the business of opening and closing the streams and a try/ catch block for handling errors. Like so:


Same result... different idiom
[ February 04, 2006: Message edited by: Garrett Rowe ]
 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much for your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!