Code question:
public class SpecialException extends Exception {
public SpecialException(String message) {
super(message);
System.out.println(message);
}
}
public class ExceptionTest {
public static void main(String[] args) {
try {
doSomething();
}
catch (SpecialException e) {
System.out.println(e);
}
}
static void doSomething() throws SpecialException {
int[] ages = new int[4];
ages[4] = 17;
doSomethingElse();
}
static void doSomethingElse() throws SpecialException {
throw new SpecialException(“Thrown at the end of doSomething() method”);
}
}
Output:
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 4
at ExceptionTest.doSomething(ExceptionTest.java:13)
at ExceptionTest.main(ExceptionTest.java:5)
/home/renko/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
D – Option C
D is correct. If you use ages[3] = 17; then it will output “Thrown at end of doSomething() method”.
static void doSomething() throws SpecialException {
int[] ages = new int[4];
ages[3] = 17;
doSomethingElse();
Output:
Thrown at the end of doSomething() method
SpecialException: Thrown at the end of doSomething() method
BUILD SUCCESSFUL (total time: 0 seconds)
d
tested
Code question:
public class SpecialException extends Exception {
public SpecialException(String message) {
super(message);
System.out.println(message);
}
}
public class ExceptionTest {
public static void main(String[] args) {
try {
doSomething();
}
catch (SpecialException e) {
System.out.println(e);
}
}
static void doSomething() throws SpecialException {
int[] ages = new int[4];
ages[4] = 17;
doSomethingElse();
}
static void doSomethingElse() throws SpecialException {
throw new SpecialException(“Thrown at the end of doSomething() method”);
}
}
Output:
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 4
at ExceptionTest.doSomething(ExceptionTest.java:13)
at ExceptionTest.main(ExceptionTest.java:5)
/home/renko/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)