Consider the following program:
import java.lang.*; class Exception1 extends IllegalArgumentException {} class Exception2 extends IllegalArgumentException {} class BaseClass { void foo() throws IllegalArgumentException { throw new IllegalArgumentException(); }//from w w w . j a va2 s .c o m } class DeriClass extends BaseClass { public void foo() throws IllegalArgumentException { throw new Exception1(); } } class DeriDeriClass extends DeriClass { public void foo() { // LINE A throw new Exception2(); } } public class Main { public static void main(String []args) { try { BaseClass base = new DeriDeriClass(); base.foo(); } catch(RuntimeException e) { System.out.println(e); } } }
Which one of the following options correctly describes the behavior of this program?
a)
It is not necessary to provide an Exception thrown by a method when the method is overriding a method defined with an exception (using the throws clause).
Hence, the given program will compile successfully and it will print Exception2.