-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUser_Defined_Exception_Demo.java
More file actions
53 lines (46 loc) · 2.05 KB
/
User_Defined_Exception_Demo.java
File metadata and controls
53 lines (46 loc) · 2.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
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Program: User Defined Exception Demo
* Description: A Java program to demonstrate the creation and handling of a custom (user-defined) exception.
* The program asks for an odd number and throws an exception if an even number is entered.
* Author: Amey Thakur
* Reference: https://github.com/Amey-Thakur/OOPM-JAVA-LAB
*/
import java.util.Scanner;
// Custom Exception Class extending Exception
class CustomNumberException extends Exception {
// Constructor receiving string message
public CustomNumberException(String s) {
// Call constructor of parent Exception class
super(s);
}
}
public class User_Defined_Exception_Demo {
// Main method declares that it might throw our custom exception
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("---------------------------------------------");
System.out.println(" User Defined Exception Demo");
System.out.println("---------------------------------------------");
try {
System.out.print("Please enter an ODD number: ");
int num = scanner.nextInt();
// Check if the number is even
if (num % 2 == 0) {
// Throw custom exception if condition (even number) is met
throw new CustomNumberException("Error: You entered an EVEN number. An ODD number was expected.");
} else {
System.out.println("Success! You entered an ODD number: " + num);
}
} catch (CustomNumberException e) {
// Catch and handle the custom exception
System.out.println("\n[Exception Caught] " + e.getMessage());
} catch (Exception e) {
// Catch any other generic exceptions (like invalid input)
System.out.println("\n[Generic Exception] " + e);
} finally {
// Always close scanner
scanner.close();
System.out.println("---------------------------------------------");
}
}
}