-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomException.java
More file actions
40 lines (40 loc) · 1.05 KB
/
Copy pathCustomException.java
File metadata and controls
40 lines (40 loc) · 1.05 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
package javalab;
import java.util.Scanner;
class AgeException extends Exception
{
public AgeException(String message)
{
super(message);
}
}
public class CustomException
{
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();
}
}
}