- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception.java
More file actions
Latest commit
20 lines (19 loc) · 574 Bytes
/
Copy pathexception.java
File metadata and controls
20 lines (19 loc) · 574 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Question 5: Custom Exception
classInvalidAgeExceptionextendsException {
publicInvalidAgeException(Stringmessage) {
super(message);
}
}
publicclassexception {
publicstaticvoidmain(String[] args) {
intage = 16; // Example age
try {
if (age < 18) {
thrownewInvalidAgeException("Age must be 18 or above to vote.");
}
System.out.println("You are eligible to vote.");
} catch (InvalidAgeExceptione) {
System.out.println(e.getMessage());
}
}
}