-
Notifications
You must be signed in to change notification settings - Fork 0
/
Custom_Exception.java
33 lines (29 loc) · 995 Bytes
/
Custom_Exception.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
import java.util.Scanner;
@SuppressWarnings("serial")
class AgeException extends Exception {
public AgeException(String message) {
super(message);
}
}
public class Custom_Exception {
public static void validateAge(int age) throws AgeException {
if (age < 18) {
throw new AgeException("Age must be at least 18 years old.");
}
System.out.println("Age is valid: " + age);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Eligibility Test for Election Voting");
System.out.print("Enter your age: ");
int age = scanner.nextInt();
try {
validateAge(age);
} catch (AgeException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("I am finally block ...I am always here...");
scanner.close();
}
}
}