Program #9 Custom Exception
Program #9 Custom Exception
Develop a JAVA program to raise a custom exception (user defined exception) for DivisionByZero
using try, catch, throw and finally.
Java Code
super(message);
if (denominator == 0) {
int denominator = 0;
try {
} catch (DivisionByZeroException e) {
} finally {
System.out.println("Finally block executed");
In this program:
● The DivisionByZeroException class is a custom exception that extends the Exception class.
● The divide method performs division and throws the custom exception if the denominator is
zero.
● In the main method, we attempt to divide and catch the custom exception if it occurs.
The finally block is used for code that must be executed, whether an exception is thrown or not.
When you run this program with a denominator of 0, it will throw the DivisionByZeroException, catch it,
print the error message, and then execute the finally block.
Output
$ java CustomExceptionDemo