X452: Exception Handling - Basics

"Determine which exception is needed for each case and put them in the corresponding catch statement with the appropriate variable. Do NOT use the general exception handler Exception for any one. Possible Exceptions: IOException, ArithmeticException, StringIndexOutOfBoundsException, NullPointerException, ArrayIndexOutOfBoundsException"

Your Answer:

x
 
1
public String catchErrors(int choice) {
2
switch (choice) {
3
    case 1:
4
        try {
5
            String word;
6
            word = null;
7
            word.charAt(0);
8
        } catch () { //TODO
9
            return e.toString();
10
        }
11
        break;
12
    case 2:
13
        try {
14
            int[] array = new int[5];
15
            array[5] = 11;
16
        } catch () { //TODO
17
            return e.toString();
18
        }
19
        break;
20
    case 3:
21
        try {
22
            int ans = 7 / 0;
23
        } catch () { //TODO
24
            return e.toString();
25
        }
26
        break;
27
}
28
return null;
29
}
30

Feedback

Your feedback will appear here when you check your answer.