Java OCA OCP Practice Question 3200

Question

Consider the following program:

public class Main {
        public static void foo() {
                try {
                        throw new ArrayIndexOutOfBoundsException();
                } catch(ArrayIndexOutOfBoundsException oob) {
                        throw new Exception(oob);
                }/*from w  w w  .  j av  a  2s. c  o  m*/
        }
        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