0% found this document useful (0 votes)
21 views2 pages

Program #9 Custom Exception

The document presents a Java program that defines a custom exception, DivisionByZeroException, to handle division by zero errors. It includes a divide method that throws this exception when the denominator is zero and demonstrates exception handling in the main method. The program outputs an error message when the exception is caught and executes a finally block regardless of whether an exception occurs.

Uploaded by

fevawov660
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Program #9 Custom Exception

The document presents a Java program that defines a custom exception, DivisionByZeroException, to handle division by zero errors. It includes a divide method that throws this exception when the denominator is zero and demonstrates exception handling in the main method. The program outputs an error message when the exception is caught and executes a finally block regardless of whether an exception occurs.

Uploaded by

fevawov660
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

9.

Develop a JAVA program to raise a custom exception (user defined exception) for DivisionByZero
using try, catch, throw and finally.

Java Code

// Custom exception class

class DivisionByZeroException extends Exception {

public DivisionByZeroException(String message) {

super(message);

public class CustomExceptionDemo {

// Method to perform division and throw custom exception if denominator is zero

static double divide(int numerator, int denominator) throws DivisionByZeroException {

if (denominator == 0) {

throw new DivisionByZeroException("Cannot divide by zero!");

return (double) numerator / denominator;

public static void main(String[] args) {

int numerator = 10;

int denominator = 0;

try {

double result = divide(numerator, denominator);

System.out.println("Result of division: " + result);

} catch (DivisionByZeroException e) {

System.out.println("Exception caught: " + e.getMessage());

} 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

You might also like