Creating and Using Custom Exceptions
Exception handling is an important concept or feature in the Java programming language that helps developers control or manage runtime errors in a proper way without damaging a software application. It provides a wide range of built-in exceptions, including the main Java exception errors, NullPointerException, ArithmeticException, and IOException. However, in real-time applications, these standard exceptions are often not effective in indicating a business-related problem.

To handle these types of conditions, Java allows developers to create custom exceptions, which are user-defined exception classes tailored to specific application requirements. User-defined custom exception classes make code easier to read, easier to maintain, and behave according to commercial-purpose logic.
What are custom exceptions in Java programming?
Custom exceptions in a Java program are user-defined classes manually created by the programmer. They enhance the hierarchy of exception classes in Java programming. Java custom exceptions behave like standard exceptions, but custom exceptions are instances designed to represent special error conditions. For example,
- A banking payment processing system might require an InsufficientBalanceException.
- A legal, government, or commercial registration system might require an InvalidUserInputException.
- An online e-learning education or other teaching platform might use a CourseNotFoundException.
Instead of using a generic exception in the Java language, a custom exception makes program code more expressive and easier to debug.
Types of Exceptions in Java Programming.
Before a Java user creates any type of custom exception, it is crucial to understand the two main types of exception concepts in Java.
Checked Exceptions in Java.
Checked exceptions in Java programs are fully checked at program compile time. Where the Java compiler checks in the current program that these user-defined program exceptions should be caught either by applying a try-catch block or by declaring them in the program using the throws keyword.
Checked Exceptions examples.
- IOException
- SQLException
Remember, custom checked exceptions in Java programs are created by extending the Exception class.
Unchecked Exceptions in Java.
Unchecked exceptions in a Java user-defined program are displayed at program runtime and are not checked by the Java compiler. Normally, unchecked exceptions in Java programming display errors.
Unchecked Exceptions examples.
- NullPointerException
- ArrayIndexOutOfBoundsException
Custom unchecked exceptions in Java programs are created by extending the RuntimeException class.
Creating a Custom Exception in Java Programming.
Step 1 – First, define an exception class.
To create a custom exception in a Java program, Java users can simply extend either Exception or RuntimeException.
Example of a Java custom checked exception.
class InvalidTestException extends Exception {
public InvalidTestException(String message) {
super(message);
}
}
Here in the Custom Exception example.
- In this program, InvalidTestException is a user-defined custom exception.
- It extends the existing Exception class, creating it as a checked exception.
- Here, Exception passes custom message information to the parent class as a constructor in the program.
Java custom unchecked exception example.
class InvalidTestException extends RuntimeException {
public InvalidTestException(String message) {
super(message);
}
}
Using a custom exception in a Java program.
Once a user-defined custom exception is created in a Java program, the Java user can invoke it in the program by using the throw keyword.
Custom exception example.
public class CustomExceptionIllustration {
// Here we use the method to validate employee salary
static void checkSalary(int salary) throws InvalidSalaryException {
if (salary > 10000 ) {
throw new InvalidSalaryException(“Employee salary should be more than 10000.”);
} else {
System.out.println(“You may be a valid employee.”);
}
}
public static void main(String[] args) {
try {
checkSalary(999); // Invalid salary user input exception
} catch (InvalidSalaryException e) {
System.out.println(“Employee salary Exception is caught – ” + e.getMessage());
}
System.out.println(“Program is still running”);
}
}
Explanation of a custom exception.
- In this custom exception program, a checkSalary() function method has been created to validate the employee’s salary.
- In this program, if the employee’s salary is less than ₹10,000, a custom exception is thrown as follows:
- hrow new InvalidSalaryException(“Employee salary should be more than ₹10,000.”);
- This declares a method:
- throw InvalidSalaryException
- because this is a properly checked exception method.
- In the program’s main() method, the exception is handled using a try-catch block.
- Similarly, the program continues executing even after the exception is handled.
Advantages of user-defined custom exceptions in Java programming.
- This program significantly improves source code readability.
- User-defined custom exceptions clearly indicate the name and problem in a program.
- Fast programs with improved error handling features.
- This allows Java software developers to handle or manage multiple individual program errors individually.
- Helps represent large, complex business logic.
- Created custom exceptions are used in programs to align with real-world scenarios and domain-specific requirements.
- Easy large, complex program source code debugging features are provided.
- These procedures make debugging your program exception messages faster and more efficient.
Best guidelines for creating custom exceptions in Java.
- Create a custom exception in a Java program with a custom user-defined exception name parameter in a useful order. For example, InvalidLoginException.
- Provide manual priority to checked exceptions for user-defined exception conditions that can be recovered in program exceptions.
- Provide priority to unchecked exceptions for programming mistakes in the current program.
- Always declare a constructor method with an exception message.
Java Multiple Constructor Example.
class InvalidLoginException extends Exception {
public InvalidLoginException(String message) {
super(message);
}
public InvalidLoginException(String message, Throwable cause) {
super(message, cause);
}
}
Conclusion on Creating and Using Custom Exceptions.
- Custom exceptions are a powerful yet important feature in the Java programming language, providing Java programmers with the flexibility to develop robust and maintainable software application designs. By extending these existing exception classes, Java programmers and developers can create useful and application-specific program error types, which enhance clarity and control over existing programs.
- Proper use of a custom exception in any Java program not only improves the quality of the program source code but also makes debugging and maintenance of the existing program easier. Whether Java users are creating a small project program or a large-scale enterprise software application system, learning to analyze, understand, and apply a custom exception in Java is a very important learning skill for every Java programmer and developer.
