• 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

try-catch block ( where to put code to close() files )

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I studying from the book "Java 2 Sun Certified Programmer for Java (Exam 310-025) Osborne, 2001
Chapter 5 - Exception Handling
pg 186 states "A finally block encloses code that is always executed at some point after the try block, whether an exception was thrown or not."
then they say "This is the right place to close your files, release your network sockets, and perform any other cleanup your code requires"
Review question 4 on pagre 210 shows this code:

the test review question "correct answer" is that the "out.close()" call in the finally clause is NOT allowed because "any method that throws a checked exception must be called within a try clause, or the method must declare that it throws the exception"
So how come the text says that a finally clause is a good place to put file close ???
[b]how should I fix the above code to make it work?
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what the text says is you need to have a try-catch block in the finally block.
the code can be changed as follows:

[ August 21, 2002: Message edited by: zarina mohammad ]
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never understood why close() can cause an IOException, or what one could do about it other than ignore it.
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Zarina and Ron
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ron Newman:
I've never understood why close() can cause an IOException, or what one could do about it other than ignore it.


the documentation says an IO Exception can occurr when trying to close a file:

"Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or
interrupted I/O operations."


what to do? try closing it again? I wonder if there is a test to see if the file is open before trying to close it???
I'll research that.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I too don't think that closing a file handle would cause an IOException (even through it is given as a checked exception). But it might have been given for making it extendable ; in case the new implementation ( custom implementation by user) could through an IOException. Just a thought
regards Jacob
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could print a warning or spawn a log that a file might be corrupted because it wasn't possible to close it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic