Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes try-catch block ( where to put code to close() files ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "try-catch block ( where to put code to close() files )" Watch "try-catch block ( where to put code to close() files )" New topic
Author

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

david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
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?
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
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 ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I've never understood why close() can cause an IOException, or what one could do about it other than ignore it.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
thank you Zarina and Ron
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
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.
Jacob George
Ranch Hand

Joined: Jun 26, 2001
Posts: 46
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
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
You could print a warning or spawn a log that a file might be corrupted because it wasn't possible to close it.


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: try-catch block ( where to put code to close() files )