Java OCA OCP Practice Question 2650

Question

Consider the following program:

import java.io.*;
public class Main {
        public static void thrower() throws Exception {
                try {
                        throw new IOException();
                } finally {
                        throw new FileNotFoundException();
                }/*from  w  ww .  j av  a2s. c  om*/
        }
        public static void main(String []args) {
                try {
                        thrower();
                } catch(Throwable throwable) {
                        System.out.println(throwable);
                }
        }
}

When executed, this program prints the following:

  • a) java.io.IOException
  • b) java .io.FileNotFoundException
  • c) java.lang.Exception
  • d) java.lang.Throwable


b)

Note

If both the try block and finally block throw exceptions, the exception thrown from the try block will be ignored.

Hence, the method thrower() throws a FileNotFoundException.

The dynamic type of the variable throwable is FileNotFoundException, so the program prints that type name.




PreviousNext

Related