Given:
public class MarkOutOfBoundsException extends ArrayIndexOutOfBoundsException {
public class Test {
public void verify(int[] arr)
“Pass Any Exam. Any Time.” – www.actualtests.com 49
Oracle 1z0-804 Exam
throws ArrayIndexOutOfBoundsException {
for (int i = 1; i <= 3; i++) {
if(arr[i] > 100)
throw new MarkOutOfBoundsException();
System.out.println(arr[i]);
}
}
public static void main(String[] args) {
int[] arr = {105,78,56};
try {
new Test().verify(arr);
} catch (ArrayIndexOutOfBoundsException |
MarkOutOfBoundsException e) {
System.out.print(e.getClass());
}
}
}
What is the result?
A.
Compilation fails.
B.
class java.lang.Array.IndexOutOfBoundException
C.
class MarkOutOfBoundException
D.
class java.lang.arrayIndexOutOfBoundException
It doesn’t make any sense to have:
catch (ArrayIndexOutOfBoundsException |
MarkOutOfBoundsException e) {
The MarkOutOfBoundException is a subtype so it is useless!
The compiler will complain!
The ansver is “A”.
– The method main cannot be declared static; static methods can only be declared in a static or top level type
– The exception MarkOutOfBoundsException is already caught by the alternative ArrayIndexOutOfBoundsException
– Syntax error, insert “}” to complete ClassBody
A
Yes, A. Exception types in the multi-catch block are related through inheritance.