- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram28_Throws.java
More file actions
Latest commit
27 lines (24 loc) · 899 Bytes
/
Copy pathProgram28_Throws.java
File metadata and controls
27 lines (24 loc) · 899 Bytes
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
// Write a java program using throws keyword to handle custom exception.
// Date : 20/03/2024, Author : Yash Wadhvani
importjava.util.Scanner;
publicclassProgram28_Throws {
staticvoidcheckAge(intage) throwsArithmeticException {
if (age < 18) {
thrownewArithmeticException("You are not a Legal Adult, Access Denied");
}
System.out.println("You are a Legal Adult, Access Granted");
}
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
System.out.println("Enter your Age :");
intage = sc.nextInt();
sc.close();
try {
checkAge(age);
} catch (ArithmeticExceptione) {
System.out.println("The Code Generates the following error :");
System.out.println(e.getClass());
System.out.println(e.getMessage());
}
}
}