Custom Exceptions in Java
In this lesson: Create user-defined checked and unchecked exceptions in Java — extend Exception, throw and catch — with output. In Hindi & English.
Why custom exceptions
Java's built-in exceptions cover general errors, but sometimes you want an error that describes your program's rules — like withdrawing more money than you have. A custom exception is your own exception class with a clear name and message, making errors easier to understand and handle.
Quick exception recap
try {
int x = 5 / 0; // throws ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Caught: " + e.getMessage());
}
Exceptions are errors you can catch with try-catch. Built-in ones like ArithmeticException are useful, but custom ones let you describe your own error situations.
Creating your own exception
class InsufficientBalanceException extends Exception {
InsufficientBalanceException(String message) {
super(message); // pass message to Exception
}
}
To make a custom exception, extend Exception and add a constructor that passes a message to super. That is all it takes to have your own named error type.
Throwing and catching it
void withdraw(int balance, int amount)
throws InsufficientBalanceException {
if (amount > balance) {
throw new InsufficientBalanceException("Not enough money");
}
System.out.println("Withdrawn: " + amount);
}
// use:
try {
withdraw(100, 500);
} catch (InsufficientBalanceException e) {
System.out.println("Error: " + e.getMessage());
}
Use throw new ... to raise it, and throws in the method to declare it. The caller catches it just like any exception.
Passing a message
Passing a message to super(message) means getMessage() returns it when caught. A good message explains what went wrong — "Not enough money" is far clearer than a generic error, which helps you and other developers debug quickly.
Checked vs unchecked
- Checked (extends
Exception) — must be caught or declared withthrows. Use for recoverable situations. - Unchecked (extends
RuntimeException) — no need to declare. Use for programming errors.
Choose checked when the caller should be forced to handle the case, unchecked for bugs that should just fail.
Common mistakes
- Not passing the message to super. Then
getMessage()returns null. - Forgetting
throwsfor checked exceptions. The compiler requires it or a catch. - Confusing throw and throws.
throwraises it;throwsdeclares it. - Overusing custom exceptions. Only create them for meaningful, specific situations.
Summary
- A custom exception describes your program's own errors clearly.
- Create it by extending
ExceptionorRuntimeException. - Pass a message to
supersogetMessage()works. - Use
throwto raise it andthrowsto declare it. - Checked must be handled; unchecked need not be.
Next, learn cleanup with final, finally and finalize, or organise code with Java methods.
Frequently Asked Questions
What is a custom exception in Java?
InsufficientBalanceException. You create it by extending Exception (or RuntimeException), which makes error messages clearer and more meaningful.How do I create a custom exception?
Exception (checked) or RuntimeException (unchecked), and usually add a constructor that passes a message to super. Then you can throw it like any other exception.What is the difference between throw and throws?
throw actually throws an exception object at a point in the code. throws is written in a method's signature to declare that it might throw a checked exception, warning callers to handle it.What is the difference between checked and unchecked exceptions?
throws or caught — the compiler enforces this. Unchecked exceptions (extend RuntimeException) do not require this and usually signal programming bugs.