-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathNestTry.java
More file actions
32 lines (29 loc) · 1.12 KB
/
NestTry.java
File metadata and controls
32 lines (29 loc) · 1.12 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
package chapter10;
public class NestTry {
public static void main(String[] args) {
try {
int a = 2;// args.length;
/*
* If no command line args are present the following statement
* will generate a divide by zero exception
* */
int b = 42 / a;
System.out.println("a = " + a);
try { //nested try block
/*
* If one command line arg is used then a divide by zero exception
* will be generated by the following code*/
if (a == 1) a = a / (a - a); //division by zero
/*If two command line args are used then generate an out of bounds exception*/
if (a == 2) {
int c[] = {1};
c[42] = 99; //generate out of bounds exception
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array Index out of bounds " + e);
}
} catch (ArithmeticException e) {
System.out.println("Divide by zero " + e);
}
}
}