Given:
public class Main {
public static void main(String[] args) {
try {
doSomething();
}
catch (SpecialException e) {
System.out.println(e);
}}
static void doSomething() {
int [] ages = new int[4];
ages[4] = 17;
doSomethingElse();
}
static void doSomethingElse() {
throw new SpecialException(“Thrown at end of doSomething() method”); }
}
What is the output?
A.
SpecialException: Thrown at end of doSomething() method
B.
Error in thread “main” java.lang.
ArrayIndexOutOfBoundseror
C.
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 4
at Main.doSomething(Main.java:12)
at Main.main(Main.java:4)
D.
SpecialException: Thrown at end of doSomething() method at
Main.doSomethingElse(Main.java:16)
at Main.doSomething(Main.java:13)
at Main.main(Main.java:4)
Explanation:
The following line causes a runtime exception (as the index is out of bounds):
ages[4] = 17;
A runtime exception is thrown as an ArrayIndexOutOfBoundsException.
Note: The third kind of exception (compared to checked exceptions and errors) is the runtime
exception. These are exceptional conditions that are internal to the application, and that the
application usually cannot anticipate or recover from. These usually indicate programming bugs,
such as logic errors or improper use of an API.
Runtime exceptions are not subject to the Catch or Specify Requirement. Runtime exceptions are
those indicated by RuntimeException and its subclasses.
Lousy question, answer you will get is something like:
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Unreachable catch block for SpecialException. This exception is never thrown from the try statement body
at Main(Q54.java:8)