What will be printed when the program is compiled and run?
class MyClass<E extends Exception> { public void throwOne(E e) throws E { throw e;// w ww .j a v a 2 s. c om } } class Exception1 extends Exception { Exception1(String str) { super(str); } } public class Main { public static void main(String[] args) { MyClass<Exception1> tantrum = new MyClass<Exception1>(); try { tantrum.throwOne(new Exception1("MyClass thrown.")); } catch (Exception1 te) { System.out.println(te.getMessage()); } } }
Select the one correct answer.
(c)
The type parameter E in the class MyClass has the upper bound Exception, and the method throwOne()
can throw an exception that is a subtype of Exception.
The generic MyClass class is instantiated correctly in the main()
method, as is the non-generic class Exception1 that is a subtype of Exception.