The Two Categories: Checked and Unchecked Exceptions
What you'll learn: You'll understand Java's fundamental split between exceptions that must be declared and those that don't require declaration.
The Core Distinction
Java divides exceptions into two categories based on whether the compiler enforces handling rules:
Checked exceptions are exceptions the compiler actively monitors. If a method can throw a checked exception, you must either catch it with a try-catch block or declare it in the method signature using throws. The compiler won't let you ignore these—it checks at compile-time that you've dealt with them somehow.
Unchecked exceptions are exceptions the compiler doesn't enforce handling for. You can catch them if you want, but you're never required to declare them or handle them. These typically represent programming errors or conditions you couldn't reasonably predict.
The Inheritance Rule
The category depends on the exception's position in the class hierarchy:
- Checked: All subclasses of
Exception(exceptRuntimeExceptionand its descendants) - Unchecked: All subclasses of
RuntimeException, plus all subclasses ofError
Think of checked exceptions as expected problems you should plan for (like a file not existing), while unchecked exceptions represent unexpected programming mistakes (like dividing by zero or accessing a null reference).
Why Two Categories?
Java's designers wanted to force programmers to acknowledge serious, recoverable problems (checked) while avoiding clutter from handling every possible programming bug (unchecked). This distinction shapes how you write every method that involves risk.
Key Takeaway: Checked exceptions must be caught or declared with throws; unchecked exceptions (RuntimeException and Error subclasses) require no compile-time handling.