Which statement about the following program is correct?
package mypkg;/* w w w. j a v a 2 s.c om*/ public class Main { class Exception1 extends RuntimeException {} class Exception2 extends RuntimeException {} class Exception1 extends RuntimeException {} public int m() { try {} catch (Exception1 d) { d = new RuntimeException(); throw d; } catch (Exception2 | Exception1 e) { e = new RuntimeException(); throw e; } return 3; } public static void main(String... wand) throws RuntimeException{ new Main().m(); } }
m()
.m()
.m()
.D.
The catch variable d is of type Exception1 that cannot be assigned an instance of the superclass RuntimeException without an explicit cast.
The first catch block does not compile in m()
.
The second catch block also does not compile, albeit for a slightly different reason.
A catch variable in a multi-catch block must be effectively final because the precise type is not known until runtime.
The compiler does not allow the variable e to be reassigned.
Option D is the correct answer.
The first catch block does allow the catch variable d to be reassigned, it just must be to a class that inherits Exception1 or is an instance of Exception1.