Java OCA OCP Practice Question 2243

Question

Consider the following program:

import java.io.FileNotFoundException;
import java.io.IOException;

public class Main {
   public static void thrower() throws Exception {
      try {// ww  w  .j  a va2 s  .c  o m
         throw new IOException();
      } finally {
         throw new FileNotFoundException();
      }
   }

   public static void main(String[] args) {
      try {
         thrower();
      } catch (Throwable throwable) {
         System.out.println(throwable);
      }
   }
}

When executed, this program prints which one of 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