Exception handling is an important part of Java programming. While Java provides many built-in exceptions, sometimes you need to create your own exception to handle specific situations. This is known as a user defined exception.
In this guide, we’ll understand how to create a custom exception using a simple and practical example: handling divide by zero.
What is an Exception?
An exception is an error that occurs during program execution and disrupts the normal flow of the program.
For example, dividing a number by zero will cause an exception.
What is a User Defined Exception?
A user defined exception is a custom exception created by the programmer to handle specific conditions.
In Java, we create custom exceptions by extending the Exception class.
class MyException extends Exception {
MyException(String message) {
super(message);
}
}
Why Use User Defined Exceptions?
- Handle specific business logic errors
- Improve code readability
- Provide meaningful error messages
- Make debugging easier
Divide by Zero Example Using User Defined Exception
Let’s create a custom exception that will be thrown when a number is divided by zero.
import java.util.Scanner;
// Custom Exception
class DivideByZeroException extends Exception {
DivideByZeroException(String message) {
super(message);
}
}
class Test {
static int divide(int a, int b) throws DivideByZeroException {
if (b == 0) {
throw new DivideByZeroException("Error: Cannot divide by zero");
}
return a / b;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
try {
int result = divide(a, b);
System.out.println("Result: " + result);
} catch (DivideByZeroException e) {
System.out.println(e.getMessage());
}
sc.close();
}
}
Output (Case 1: Valid Input)
Enter first number: 10 Enter second number: 2 Result: 5
Output (Case 2: Divide by Zero)
Enter first number: 10 Enter second number: 0 Error: Cannot divide by zero
How This Program Works
- We create a custom exception class
DivideByZeroException - If the denominator is zero, we throw the exception
- The
try-catchblock handles the error - The program continues without crashing
Key Points to Remember
- Custom exceptions extend the
Exceptionclass - Use
throwto generate exception - Use
throwsin method declaration - Always handle exceptions properly
Common Mistakes
- Forgetting to use throws keyword
- Not handling exception with try-catch
- Using generic Exception instead of custom one
Best Practices
- Use meaningful exception names
- Provide clear error messages
- Avoid overusing custom exceptions
FAQ
What is a user defined exception in Java?
A custom exception created by programmer to handle specific errors.
How do you create a custom exception?
By extending the Exception class.
What is throw keyword?
It is used to explicitly throw an exception.
Conclusion
User defined exceptions help you handle errors in a more meaningful and controlled way. The divide by zero example is a simple but powerful way to understand how custom exceptions work in Java.
Practice creating your own exceptions to improve your understanding of exception handling.

Comments
Post a Comment