Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • 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?
 
zarina mohammad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thank you Zarina and Ron
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • 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.
 
Jacob George
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic