User Defined Custom Exceptions in Java
In this tutorial, we will discuss user-defined custom exceptions with examples.
Introduction
In Java, we have proactively characterised, Exception classes, for example, ArithmeticException, NullPointerException, ArrayoutOfBound and so on. These built-in exceptions are now set off on pre-characterized conditions; for example, dividing a number by zero triggers to ArithmeticException. In the previous session, we found the built-in exceptions and resolved them using the try-catch block and throws on the output screens.
We can make our exception class in Java and try that Exception utilising the try-catch block. These exceptional cases are known as client characterised or custom exemptions.
To comprehend this instruction, we ought to have the essential information on try-catch, throw keywords and the basics of Exceptions.
An Exception is an event or issue during the program's execution. It will disturb the program's entire code and can't execute entirely, and get terminated instantly.
In Java programming language, we have a specific facility to create our exception-derived classes of Exception classes. Creating our Exception is known as a custom exception or user-defined Exception. Generally, User-defined exceptions are used to customise the Exception based on the user requirements. From the above statements, We can say that a Custom exception is creating our Exception and throwing the appeared Exception on the console or output screen by using the 'throw' keyword.
Custom exceptions in Java programming language are the exceptions that a developer creates to encounter the application's particular requirements. That's why it is also known as a user-defined Exception in Java.
For example:
1. Banking Application: Every customer who is willing to open their accounts must have at least 18 years old. But if the customer's age is less than 18 years, then the program for the specific requirements throws a custom exception by indicating the suggestions.
2. Voting age: If a citizen’s age is lesser than 18 years in India, then the program for this specific requirement throws "Invalid age" as a custom exception
Let's take an example of a user-defined Exception that is DemoException by extending the Exception class.
In the below example, we have created our Exception, which extends from Exception Class.
Purpose of Custom Exceptions
In Java programming java, there are a lot of exceptions occurred during the execution of the program. Sometimes, we must create exceptions, which we call custom exceptions.
There are specific reasons behind the use of custom exceptions. The following are a few reasons ….
- To catch and give a particular treatment to a few sets of existing exceptions.
- Business logic exceptions: There are many exceptions involved in Java related to own requirements and field. Business logic exceptions are related to business logic and workflow. It is beneficial for the users or the developers to understand the exact problem of the domain.
To create our won exceptions, we need to extend the Exception class that belongs to the lang package in Java.
Example:
In this example,we will pass a string to the constructor, a superclass of exception obtained using the "getMessage()” function on the object created.
Program:
// Demoexception Class that represents user-defined Exception
class DemoException extends Exception {
public DemoException(String s)
{
// it will call the constructor of the parent exception class
super(s);
}
}
// A Class that uses the above DemoException
public class Tutorialandexample {
public static void main(String args[])
{
try {
// Throw an object of user-defined Exception
throw new DemoException(“Tutorialandexample");
}
catch (DemoException ex) {
System.out.println("Caught an Exception");
// Displaying the message from DemoException object System.out.println(ex.getMessage());
}
}
}
Output
Caught an Exception
Tutorialandexample
From the above code, we can clear that the constructor of DemoException requires a string as its argument. The argument is passed to the base class exception’s constructor using the super() keyword. The super key is not required if we don't want to give a parameter.
Program:
// Demo Class to represents user-defined exception
class DemoException extends Exception {
}
// A Jtp Class that uses the above DemoException
public class Jtp{
public static void main(String args[])
{
try {
// It will throw an object of custom Exception
throw new DemoException();
}
catch (DemoException ex) {
System.out.println("Caught an Exception");
System.out.println(ex.getMessage());
}
}
}
Output
Caught an Exception
null