-
Notifications
You must be signed in to change notification settings - Fork 0
/
slip29A.java
30 lines (26 loc) · 952 Bytes
/
slip29A.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
import java.util.Scanner;
class AgeOutOfRangeException extends Exception {
public AgeOutOfRangeException(String message) {
super(message);
}
}
public class VotingEligibilityChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter candidate's age: ");
int age = scanner.nextInt();
if (age < 18) {
throw new AgeOutOfRangeException("Candidate is not eligible to vote due to age.");
} else {
System.out.println("Candidate is eligible to vote.");
}
} catch (AgeOutOfRangeException e) {
System.err.println("Error: " + e.getMessage());
} catch (Exception e) {
System.err.println("Error: Invalid input or other exception.");
} finally {
scanner.close();
}
}
}