Javatpoint Logo
Javatpoint Logo

customized exception in Java

In Java, exceptions are a fundamental part of handling errors and unexpected events in a program. However, the standard Java exceptions may not always be sufficient to handle all the different types of errors that can occur in a program. That is where customized exceptions come in.

Customized exceptions, also known as user-defined exceptions, allow developers to define their own exceptions and use them to handle specific types of errors that are not covered by the standard exceptions. In this article, we will explore how to create and use customized exceptions in Java.

Creating Customized Exceptions

To create a customized exception, we need to define a new class that extends the built-in Exception or RuntimeException class. The Exception class is used for exceptions that are expected to be caught and handled by the calling code, while the RuntimeException class is used for exceptions that are not expected to be caught by the calling code.

Here is an example of a customized exception class:

In the above example, we have created a new class called CustomException that extends the Exception class. The constructor of the CustomException class takes a message as a parameter and passes it to the constructor of the Exception class using the super() method.

Using Customized Exceptions

Once we have created a customized exception, we can use it in our program by throwing it when an error occurs. Here is an example of how to use the CustomException class:

In the above example, we have a method called myMethod that takes an integer value as a parameter. If the value is less than zero, we throw a CustomException with the message "Value cannot be negative". The calling code can then catch the CustomException and handle it appropriately.

Handling Customized Exceptions

To handle a customized exception, we can use a try-catch block just like we would with a standard Java exception. Here is an example of how to handle the CustomException class:

In the above example, we have created an instance of the MyClass class and called the myMethod() method with a value of -1. Since the value is less than zero, a CustomException is thrown. We then catch the CustomException and print the error message.

here's an example program that demonstrates the use of a customized exception in Java:

CustomExceptionExample.java

In this program, we have a method called divideNumbers() that takes two integers as parameters and returns the result of dividing the first number by the second number. However, if the second number (the divisor) is zero, we throw a CustomException with the message "Division by zero not allowed".

In the main() method, we call the divideNumbers() method with the numbers 10 and 0. Since the second number is zero, a CustomException is thrown. We then catch the CustomException and print the error message.

When we run this program, we should see the following output:

This demonstrates how we can use a customized exception to handle specific types of errors in our Java programs.

Conclusion

Customized exceptions provide a powerful tool for handling specific types of errors in a Java program. By creating our own exception classes, we can give our programs the ability to handle errors in a more granular way, which can lead to more efficient and robust code. However, it is important to use customized exceptions judiciously and only when necessary, as too many custom exceptions can lead to code that is difficult to maintain and debug.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA