-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path8_UserDefineException.java
74 lines (50 loc) · 1.37 KB
/
8_UserDefineException.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
import java.io.*;
class DataUnderFlowException extends RuntimeException{
DataUnderFlowException(String msg) {
super(msg);
}
}
class DataOverFlowException extends RuntimeException{
DataOverFlowException(String msg) {
super(msg);
}
}
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int arr[] = new int[5];
System.out.println("Enter Integer value in Array : ");
System.out.println("Note: 0 < element < 100");
for(int i = 0; i < arr.length; i++) {
int data = 0;
try {
data = Integer.parseInt(br.readLine());
} catch(NumberFormatException nfe) {
System.out.println("Enter Integer Value : ");
data = Integer.parseInt(br.readLine());
}
if(data < 0) {
try {
throw new DataUnderFlowException("Data is less than 0");
} catch(DataUnderFlowException duf) {
System.out.println(duf.getMessage());
i--;
}
}
if(data > 100) {
try {
throw new DataOverFlowException("Data is greater than 100");
} catch(DataOverFlowException duf) {
System.out.println(duf.getMessage());
i--;
}
}
arr[i] = data;
}
System.out.println("Array Elements : ");
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] +" ");
}
System.out.println();
}
}