-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionDemoFinally.java
More file actions
39 lines (39 loc) · 1.16 KB
/
Copy pathExceptionDemoFinally.java
File metadata and controls
39 lines (39 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class ExceptionDemoFinally {
public static void genException(int what) {
int t;
int[] nums = new int[2];
System.out.println("Receiving " + what);
try {
switch(what) {
case 0:
t = 10 / what; // generate div-by-zero error
break;
case 1:
nums[4] = 4; // generate array index error.
break;
case 2:
return; // return from try block
}
}
catch (ArithmeticException exc) {
// Catch the exception.
System.out.println("Can't divide by Zero!");
return; // return from catch
}
catch (ArrayIndexOutOfBoundsException exc) {
// Catch the exception.
System.out.println("No matching element found.");
}
finally {
System.out.println("Leaving try.");
}
}
}
class FinallyDemo {
public static void main(String[] args) {
for(int i=0; i < 3; i++) {
ExceptionDemoFinally.genException(i);
System.out.println();
}
}
}