-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserException.java
36 lines (31 loc) · 903 Bytes
/
UserException.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
import java.util.Scanner;
class MyException extends Exception{
public MyException(String msg) {
super(msg);
}
}
public class UserException {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of Array : ");
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0 ; i < n; i++){
arr[i] = sc.nextInt();
}
try{
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
// System.out.println("Duplicate found: " + j);
throw new MyException("Duplicate found");
}
}
}
}catch (MyException e) {
System.out.println("Exception caught: " + e);
}catch (NumberFormatException e) {
System.out.println("Please enter a valid integer.");
}
}
}