User defined exceptions
User defined exceptions
In Java, it's possible to create our own exceptions by defining classes that extend the built-in Exception class. This process of creating custom exceptions is often referred to as user-defined exceptions. Custom exceptions allow developers to tailor exception handling to specific needs and scenarios.
For instance, let's consider Example 1, where we have a custom exception class named InvalidAgeException that extends the Exception class.
The primary purpose of custom exceptions in Java is to provide a way to define our own exception types with specific error messages. When we create a custom exception class, we can pass a custom error message to the superclass constructor, which is the Exception class in this case. Later, when we use this custom exception in our code, we can access and display this custom error message using the getMessage() method on the exception object we create.
In the following section, we'll delve deeper into how to implement and utilize custom exceptions in Java programs.
In essence, custom exceptions in Java empower developers to design exceptions that align with their application's requirements, thereby enhancing the clarity and precision of error handling.
Java's built-in exception classes encompass a wide range of common exceptional scenarios in programming. Nevertheless, there are situations where it becomes necessary to design and implement custom exceptions tailored to specific needs
Following are a few of the reasons to use custom exceptions
To intercept and apply specialized handling to a subset of predefined Java exceptions.
Business logic exceptions: These pertain to exceptions associated with the application's business rules and operational flow.
These exceptions aim to provide clear insight into the specific issue for both end-users and developers.
To create a custom exception in Java, you should extend a class from the java.lang package, typically Exception or one of its subclasses.
Example:-
We provide a string message to the constructor of the parent class, Exception, and later retrieve it using the 'getMessage()' method on the generated object.
// A Class that represents use-defined exception
class MyException extends Exception {
public MyException(String s)
{
// Call constructor of parent Exception
super(s);
}
}
// A Class that uses above MyException
public class Main {
// Driver Program
public static void main(String args[])
{
try {
// Throw an object of user defined exception
throw new MyException("CITS");
}
catch (MyException ex) {
System.out.println("WORLD");
// Print the message from MyException object
System.out.println(ex.getMessage());
}
}
}
Output:-
WORLD
CITS
In the provided code, the MyException constructor expects a string argument. This string is passed to the parent class Exception's constructor using the super() keyword. It's important to note that the Exception class's constructor can also be invoked without any parameters, and using super is not obligatory in that case.
// A Class that represents use-defined exception
class MyException extends Exception {
}
// A Class that uses above MyException
public class setText {
// Driver Program
public static void main(String args[])
{
try {
// Throw an object of user defined exception
throw new MyException();
}
catch (MyException ex) {
System.out.println("World");
System.out.println(ex.getMessage());
}
}
}
Output:-
World
null