-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExceptHand.java
88 lines (78 loc) · 1.84 KB
/
ExceptHand.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class Test {
static int div(int a, int b) {
return a / b;
}
public static void main(String args[]) {
try {
int a = args.length;
int b = 42;
b = div(a, b);
} catch (ArithmeticException e) {
System.out.println("Exception: " + e);
}
}
}
class TestExcept {
public static void getExcept() {
try {
throw new NullPointerException("Exception test");
} catch (NullPointerException e) {
System.out.println("Exception caught: " + e);
}
}
public static void main(String args[]) {
try {
getExcept();
} catch (NullPointerException e) {
System.out.println("Caught inside main: " + e);
}
}
}
class TestTwoExcept {
public static void nestedTry() {
try {
try {
throw new NullPointerException("called within the nested try");
} catch (ArithmeticException e) {
System.out.println("Caught in inener catch: " + e);
}
} catch (ArithmeticException e) {
System.out.println("Caught exception in outer catch: " + e);
} catch (NullPointerException e) {
System.out.println("Caught exception in outer catch: " + e);
}
}
public static void main(String args[]) {
nestedTry();
}
}
class TestUserExcept {
public static int div(int a, int b) throws DivByZero {
int res;
System.out.println("Division method called");
if (b == 0) {
throw new DivByZero(b);
} else {
res = a / b;
}
return res;
}
public static void main(String args[]) {
int a = 10, b = 0;
try {
int res = div(a, b);
System.out.println("Division of " + a + " and " + b + " is: " + res);
} catch (DivByZero e) {
System.out.println("Exception caught: " + e);
}
}
}
class DivByZero extends Exception {
private int divisor;
DivByZero(int a) {
divisor = a;
}
public String toString() {
return "Divide by zero attempt caught!";
}
}