0% found this document useful (0 votes)
4 views3 pages

Program 9

The document outlines the steps to create a Java program that raises a custom exception for division by zero. It includes instructions for setting up a Java project in Eclipse, creating a package and class, and writing the program code that handles the exception using try, catch, throw, and finally blocks. The document also provides expected outputs for both normal execution and when the exception is triggered.

Uploaded by

samfrancisco069
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)
4 views3 pages

Program 9

The document outlines the steps to create a Java program that raises a custom exception for division by zero. It includes instructions for setting up a Java project in Eclipse, creating a package and class, and writing the program code that handles the exception using try, catch, throw, and finally blocks. The document also provides expected outputs for both normal execution and when the exception is triggered.

Uploaded by

samfrancisco069
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

PROGRAM 9:

Develop a JAVA program to raise a custom exception (user defined


exception) for DivisionByZero using try, catch, throw and finally.

Step 1: Open Eclipse


• Open Eclipse IDE
• Click File → New → Java Project
• Project Name: Employee
• Click Finish

Step 2: Create a Package


• In Project Explorer
• Right-click on src
• Click New → Package
• Package name:
employee
• Click Finish

Step 3: Create Java Class


• Right-click on employee package
• Click New → Class
• Class Name:
Point
• Tick public static void main(String[] args)
• Click Finish
Step 4: Write the Program Code
Copy and paste the complete code below inside [Link]:
package employee;

// User-defined exception class


class DivideByZeroException extends Exception {
public DivideByZeroException(String message) {
super(message);
}
}

public class Point {

public static void main(String[] args) {


int numerator = 15;
int denominator = 2; // change to 0 to test exception

try {
if (denominator == 0) {
throw new DivideByZeroException(
"Error: Cannot divide a number by zero.");
}

int result = numerator / denominator;


[Link]("Result: " + result);
} catch (DivideByZeroException e) {
[Link]([Link]());
} finally {
[Link]("I am in final block");
}
}
}

Step 5: Run the Program


• Right-click on [Link]
• Click Run As → Java Application

PART B: Output
Output 1 (Normal Execution)
If:
int denominator = 2;
Result: 7
I am in final block

Output 2 (Exception Case)


If:
int denominator = 0;
Error: Cannot divide a number by zero.
I am in final block

You might also like