forked from NaNaDi/Programming_Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionHandling.java
More file actions
66 lines (54 loc) · 1.27 KB
/
Copy pathExceptionHandling.java
File metadata and controls
66 lines (54 loc) · 1.27 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
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
public class ExceptionHandling {
public static void main(String[] args) {
//todo:
//1.) Execute the following program. It will throw Exceptions.
//Catch the Exceptions using their proper Names till no further
//exceptions occur.
//
//2.) Taking the square root of a negative number won't cause
//the exception. Write your own Exception for this case and
//write a method which throws this exception
try{
int a = 5;
int b = 0;
int div = a/b;
}
catch(ArithmeticException e){
System.err.println("error occurred");
e.printStackTrace();
}
try{
int[] arr = new int[5];
arr[5] = 7;
}
catch(ArrayIndexOutOfBoundsException e){
System.err.println("error occurred");
e.printStackTrace();
}
try{
String one = "one";
int i = Integer.parseInt(one);
}
catch(NumberFormatException e){
System.err.println("error occurred");
e.printStackTrace();
}
try{
double c = -7.0;
double d = root(c);
System.out.println(d);
}
catch(CustomException e){
System.err.println("error occurred");
e.printStackTrace();
}
}
public static double root(double c) throws CustomException{
if(c<0){
throw new CustomException("cannot sqrt negative number");
}
else{
return Math.sqrt(c);
}
}
}