Learn Java
import com.learnjava.exceptionhandling.CustomExceptions;

Custom Exceptions in Java

@Concept

A custom exception is a class that extends Exception, making it checked, or RuntimeException, making it unchecked, created specifically to represent a problem particular to a given program's domain — InsufficientFundsException communicates far more clearly than a generic IllegalStateException ever could about exactly what went wrong in a banking application. Defining one is usually just a small class with one or more constructors that call super(message) to set the exception's descriptive text, though it can carry extra fields too, like the specific account or amount involved.

Whether to extend Exception or RuntimeException for a custom exception comes down to the same checked-versus-unchecked judgment call as any other exception. If calling code can reasonably be expected to anticipate and specifically handle this condition, checked is often the better fit; if it represents a deeper misuse or bug, unchecked usually fits better.

@HowItWorks
1

Extending Exception or RuntimeException

Determines whether the new custom exception is checked or unchecked, exactly like any built-in exception.

2

A descriptive class name

Communicates specifically what went wrong, more clearly than a generic built-in exception type could.

3

A constructor calling super(message)

Passes the descriptive text up to Exception's own constructor, so getMessage() works correctly on the new type.

4

Extra fields for structured detail

A custom exception can carry more than just a message, like the specific value that caused the problem.

5

Choosing checked or unchecked

Follows the same judgment as any exception: can callers reasonably anticipate and handle this, or does it represent a deeper bug?

@Example
class InsufficientFundsException extends Exception {
InsufficientFundsException(String message) {
super(message);
}
}
 
public class CustomExceptionDemo {
static void withdraw(double balance, double amount) throws InsufficientFundsException {
if (amount > balance) {
throw new InsufficientFundsException("Cannot withdraw " + amount + " from balance " + balance);
}
System.out.println("Withdrawal successful");
}
 
public static void main(String[] args) {
try {
withdraw(100, 150);
} catch (InsufficientFundsException e) {
System.out.println("Caught: " + e.getMessage());
}
}
}
Output
Caught: Cannot withdraw 150.0 from balance 100.0
1InsufficientFundsException extends Exception, making it a checked exception with its own descriptive, domain-specific name.
2withdraw declares throws InsufficientFundsException, since it's a checked exception the compiler requires callers to acknowledge.
3Since 150 exceeds the balance of 100, the method throws that custom exception with a message describing exactly what happened.
4main's catch block catches it specifically by its custom type and prints the descriptive message it carries.
@UseCases
Communicating a domain-specific failure clearly, like InsufficientFundsException instead of a generic exception
Carrying structured extra detail about a failure beyond just a text message
Forcing callers to specifically acknowledge and handle a particular business-logic failure, using a checked custom exception
Building a small hierarchy of related custom exceptions that share a common custom superclass

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.