Which exception classes, when inserted into the blank in the Main class, allow the code to compile?
package mypkg;/*from ww w .ja va2 s. com*/ class Exception1 extends Exception {} class Exception2 extends Exception {} public class Main { public void m() throws Exception1, Exception2 { System.out.println("No problems"); } public static void main(String[] lots) throws ___ { try { final Main p = new Main(); p.m(); } catch (Exception e) { throw e; } } }
C.
First off, the try block is capable of throwing two checked exceptions, Exception1 and Exception2.
The catch block uses the Exception class to handle this, since both have Exception as a super type.
It then re-throws the Exception.
For this reason, Exception would be appropriate in the blank, making the first statement correct.
The compiler is also smart enough to know that there are only two possible subclasses of Exception that can actually be thrown in the main()
method, so declaring Exception1 and Exception2 together also allows the code to compile, making the third statement correct.
The second statement, only inserting Exception1, would not allow the code to compile because the main()
method could throw a checked Exception2 that was not handled.
Option C is the correct answer.