Java OCA OCP Practice Question 2231

Question

Consider the following program:

public class Main {
   public static void foo() {
       try {/* w  w  w  .  j a  va2 s .  c  o m*/
           throw new ArrayIndexOutOfBoundsException();
        } catch(ArrayIndexOutOfBoundsException oob) {
            throw new Exception(oob);
        }
    }

     public static void main(String []args) {
         try {
             foo();
         } catch(Exception re) {
             System.out.println(re.getCause());
         }
     }
}

Which one of the following options correctly describes the behavior of this program?

  • A. java.lang.Exception
  • B. java.lang.ArrayIndexOutOfBoundsException
  • C. class java.lang.IllegalStateException
  • d. this program fails with compiler error(s)


d.

Note

the foo() method catches ArrayIndexOutOfBoundsException and chains it to an Exception object.

however, since Exception is a checked exception, it must be declared in the throws clause of foo().




PreviousNext

Related