Given:
public class Test {
void display(String[] arr) {
try {
System.out.print(arr[2]);
} catch(ArrayIndexOutOfBoundsException |
NullPointerException e) {
e = new Exception();
throw e;
}
}
public static void main(String[] args) throws Exception {
try {
String[] arr = {“Unix”,”Solaris”,null};
new Test().display(arr);
} catch(Exception e) {
System.err.print(e.getClass());
}
}
}
What is the result?
A.
Null
B.
class java.lang.ArraylndexOutOfBoundsException
C.
class java.lang.NullPointerException
D.
class java.lang.Exception
E.
Compilation fails.
Explanation:
error: incompatible types
e = new Exception();
required: RuntimeException
found: Exception
Type mismatch: cannot convert from Exception to RuntimeException
&
The parameter e of a multi-catch block cannot be assigned
+1
E.
e = new Exception();
“Type mismatch: cannot convert from Exception to RuntimeException”
e = new ArrayIndexOutOfBoundsException();
With a RuntimeException the compiler says:
“The parameter e of a multi-catch block cannot be assigned”
E
A
E. If the line “e = new Exception();” is remarked, the answer would be A.
E, can’t re-assign multicatch parameter, and even if you could, RuntimeException reference e is not compatable with Exception object.