-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomException.java
More file actions
46 lines (34 loc) · 1.26 KB
/
Copy pathCustomException.java
File metadata and controls
46 lines (34 loc) · 1.26 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
import java.util.Scanner;
class AgeException extends Exception {
public AgeException(String message) {
super(message);
}
}
public class VotingEligibility {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String continueChoice;
do {
System.out.println("Eligibility Test for Election Voting");
System.out.print("Enter your age: ");
int age = sc.nextInt();
try {
checkAge(age);
System.out.println("Age is valid: " + age);
} catch (AgeException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("I am finally block ... I am always here...");
}
System.out.print("Do you want to continue? Type Y or N: ");
continueChoice = sc.next();
} while (continueChoice.equalsIgnoreCase("Y"));
sc.close();
}
// Step 7: Method to check age validity
public static void checkAge(int age) throws AgeException {
if (age < 18) {
throw new AgeException("Age must be at least 18 years old.");
}
}
}