Checked and Unchecked Exceptions
- The two kinds. Java splits exceptions: checked ones must be declared or handled, unchecked RuntimeExceptions may fly free.
- throws. The throws clause names the checked exceptions a method may raise: failure as part of the signature.
- try/catch/finally. A try block guards risky code, catch handles the named type, and finally runs no matter what.
Error Propagation
- Let it rise. Most methods should propagate: catch only where you can genuinely handle, usually near the top.
- Wrapping exceptions. Wrapping a low-level exception in a domain one keeps the cause chained and the abstraction honest.
- try-with-resources. try-with-resources closes anything AutoCloseable automatically: files and connections that cannot leak.
Reliable Exception Design
- Messages that help. An exception message names what was expected and what arrived: the 2am reader’s first clue.
- Never swallow. An empty catch block swallows the failure: the bug survives, the evidence dies.
- Exceptions versus results. Expected outcomes: not found, invalid input: often suit Optional or result objects better than exceptions.
Before and after this level
- Before: Level 4: Collections & Generics: Lists, Sets and Maps, Generic Types, Choosing Appropriate Collections.
- Next: Level 6: Functional Java: Lambdas, Streams, Functional Data Processing.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does Java Level 5: Exceptions cover?
Checked and Unchecked Exceptions, Error Propagation, Reliable Exception Design: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 4: Collections & Generics, which covers Lists, Sets and Maps, Generic Types, Choosing Appropriate Collections.