<%@ include file="../../ga4.jsp" %> Java Finally Keyword - Quiz Explanation

Finally keyword in Java - Quiz Explanation

The correct answers are indicated below, along with text that explains the correct answers.
 
1. Examine the following snippet of code. What do you expect the output to be?
try {
   Integer myInteger = new Integer("$");
} catch (Exception x) {
   System.out.println("error");
   return;
} finally {
   System.out.println("clean up");
}

Please select the best answer.
  A. error
clean up
  B. error
  C. No error at all!
  The correct answer is A.
This snippet will throw an exception, because a dollar sign cannot be converted into a number.
But that's not the end of the story.
Regardless of what occurs in the exception clause, even if there is an explicit return statement, the finally clause, if there is one, will be executed as well.

2. Code within a finally block is
Please select the best answer.
  A. Always executed
  B. Executed unless there is a return statement in the try or catch block preceding it
  C. Depends on the version of the Java virtual machine.
  The correct answer is A.
A finally block is always executed, no matter what.

3. What is wrong with the following code? Assume that the class MyFileObject is a valid class that is capable of representing a file in the file system, whose constructor takes a String indicating the file name and does not throw an exception, and that defines the methods open() and close(), and that open() might throw an IOException.
MyFileObject obj = new MyFileObject("test.in");
try {
   obj.open();
} catch(IOException) {
   System.out.println("an error occurred");
}

finally {
   obj.close();
}
Please select the best answer.
  A. The try clause is not defined correctly
  B. The catch clause is not defined correctly
  C. The finally clause is not defined correctly
  The correct answer is B.
The try and finally clauses are fine. The problem is that the catch clause must provide a parameter for the exception object, as in: catch (IOException x)