Lesson 15: Custom Exceptions & Checked vs Unchecked
16 Jul 2026 1 min Swarnil Singhai
Checked exceptions force callers to handle or declare them (compile-time enforced); unchecked ones (RuntimeException) don't — use each deliberately.
Real-time example
A banking API defines InsufficientFundsException (checked) so every caller is forced to explicitly handle a declined withdrawal.
class InsufficientFundsException extends Exception {
InsufficientFundsException(String msg) { super(msg); }
}
void withdraw(double amt, double balance) throws InsufficientFundsException {
if (amt > balance)
throw new InsufficientFundsException("Balance too low for Rs " + amt);
}
What's happening
Because it extends Exception (not RuntimeException), any caller of withdraw must either catch it or declare throws themselves.
Reserve checked exceptions for recoverable business conditions; use unchecked for programmer errors (bad args, null).
Advertisement