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?
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