Custom Exceptions in Java
What Is a Custom Exception?
A Custom Exception is a user-defined class that extends either (checked) Exception or RuntimeException (unchecked).
It allows developers to create meaningful and descriptive error messages tailored to their application’s context.
Example :
If you are building a banking system, instead of throwing a generic Exception, you can create your own
throw new InsufficientFundsException("Balance is too low for this withdrawal!");This message clearly explains what went wrong — much better than “Exception occurred”.
Why Use Custom Exceptions?
Custom exceptions make your code more,
- Readable — clearly states the problem instead of generic messages.
- Maintainable — makes debugging easier and logs more meaningful.
- Modular — keeps error handling close to your domain logic.
- Reusable — can be caught and handled separately across layers (controller, service, etc.).
Types of Custom Exceptions
Checked Custom Exception
Get Keerthi Gopalavannan’s stories in your inbox
Join Medium for free to get updates from this writer.
If you want the compiler to ensure handling, extend the Exception class.
public class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}Use it like this
public void registerUser(int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException("User must be 18 or older to register");
}
}And handle it
try {
registerUser(16);
} catch (InvalidAgeException e) {
System.out.println("Error: " + e.getMessage());
}Checked exceptions are checked at compile-time, so the method must handle or declare them.
Unchecked Custom Exception
If you don’t want the compiler to enforce handling, extend RuntimeException.
public class InvalidInputRuntimeException extends RuntimeException {
public InvalidInputRuntimeException(String message) {
super(message);
}
}Use it like
public void processData(String input) {
if (input == null) {
throw new InvalidInputRuntimeException("Input cannot be null");
}
}Unchecked exceptions are checked at runtime. Use them for programmer errors or logic validation.
Best Practices for Custom Exceptions
✔Use meaningful names (Eg: UserNotFoundException , InvalidTransactionException)
✔ Keep constructors simple — usually, a message and a cause are enough.
✔ Don’t create unnecessary custom exceptions — only when built-in ones don’t fit.
✔ Log exceptions once — avoid multiple redundant logs.
✔Add contextual messages to make debugging easier.
Real-World Example
building an online payment system
// 1. Base Exception
public class PaymentException extends Exception {
public PaymentException(String message) {
super(message);
}
}
// 2. Specific Exception
public class InsufficientBalanceException extends PaymentException {
public InsufficientBalanceException(String message) {
super(message);
}
}Usage
public void processPayment(double balance, double amount) throws PaymentException {
if (balance < amount) {
throw new InsufficientBalanceException("Not enough funds to complete the payment");
}
System.out.println("Payment successful!");
}Handling:
try {
processPayment(200.0, 500.0);
} catch (PaymentException e) {
System.out.println("Transaction failed: " + e.getMessage());
}Custom exceptions give your Java applications more clarity, control, and context when something goes wrong. They help bridge the gap between technical errors and business-level understanding, making debugging and maintenance far easier.










