-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTut25.java
More file actions
54 lines (43 loc) · 1.01 KB
/
Tut25.java
File metadata and controls
54 lines (43 loc) · 1.01 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
package tutorial;
//making Custom Exception
class NotFoundNumber extends Exception {
private int no;
public NotFoundNumber(int no) {
this.no = no;
}
// auto generated from eclipse ide
@Override
public String getMessage() {
return "The Number you are Searching is not found %d".formatted(no);
}
@Override
public String toString() {
return "Not such number found";
}
@Override
public void printStackTrace() {
super.printStackTrace();
}
}
public class Tut25 {
public static void Number() throws NotFoundNumber {
// throwing the exception
int[] arr = { 1, 2, 3 };
int index = 3;
if (index > arr.length - 1) {
throw new NotFoundNumber(index);
} else {
System.out.println(arr[index]);
}
}
// throws means saying that be ready there will be possibilty of exception
public static void main(String[] args) throws NotFoundNumber {
try {
Number();
} catch (NotFoundNumber e) {
System.out.println(e.getMessage());
System.out.println(e.toString());
e.printStackTrace();
}
}
}