How many constructors in Exception1 compile in the following class?
package mypkg;/* w ww . j a va 2 s. co m*/ public class Exception1 extends Exception { public Exception1() { super("Wrong"); } public Exception1(String message) { super(new Exception(new Exception1())); } public Exception1(Exception cause) {} }
D.
The Exception class contains multiple constructors, including one that takes Throwable, one that takes String, and a no-argument constructor.
The first Exception1 constructor compiles, using the Exception constructor that takes a String.
The second Exception1 constructor also compiles.
The two statements, super()
and new Exception()
, actually call the same constructor in the Exception class, one after another.
The last Exception1 compiles with the compiler inserting the default no-argument constructor super()
, because it exists in the Exception class.
All of the constructors compile, and Option D is the correct answer.