-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomExceptionTest.java
More file actions
54 lines (41 loc) · 1.14 KB
/
CustomExceptionTest.java
File metadata and controls
54 lines (41 loc) · 1.14 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
46
47
48
49
50
51
52
53
54
package exhand;
import java.util.Scanner;
public class CustomExceptionTest {
public static void main(String[] args) {
Scanner sc = null;
CustomExceptionTest obj = new CustomExceptionTest();
try {
sc = new Scanner(System.in);
System.out.println("Enter your Age: ");
int age = sc.nextInt();
obj.checkEligibleAgeForVote(age);
} catch (InvalidAgeException e) {
e.printStackTrace();
} finally {
sc.close();
}
// Scanner sc = null;
// try {
// System.out.print( "Enter your Age: " );
// sc = new Scanner( System.in );
//
// CustomExceptionTest obj = new CustomExceptionTest( );
// int age = sc.nextInt( );
// obj.checkEligibleAgeForVote( age );
//
// } catch ( InvalidAgeException e ) {
// e.printStackTrace( );
//
// } finally {
// sc.close( );
// System.out.println( "Resource Closed." );
// }
}
public void checkEligibleAgeForVote(int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException("You are not eligible to vote. Age: " + age);
} else {
System.out.println("You can vote. Age: " + age);
}
}
}