Exceptions
Java's exception mechanism separates the happy path from error-handling code, making intent clearer. The critical distinction — checked vs. unchecked exceptions — defines a contract between a method and its callers. Modern Java code leans toward unchecked exceptions, and
try-with-resources(Java 7+) eliminates entire categories of resource-leak bugs.
What You'll Find Here
Notes are being added. Planned topics:
| Topic | Description |
|---|---|
| Exception Hierarchy | Throwable → Error vs. Exception; checked vs. unchecked (RuntimeException). |
| try / catch / finally | Multi-catch |, finally guarantees, try-with-resources. |
| Custom Exceptions | Creating domain-specific exceptions; adding context and cause chaining. |
| Best Practices | Fail-fast, don't swallow exceptions, when to use checked vs. unchecked. |
Learning Path
- Exception Hierarchy — understand the
Throwabletree; know thatErrorshould never be caught. - try / catch / finally — learn
try-with-resourcesfirst since it eliminates most explicitfinallyblocks. - Custom Exceptions — wrapping causes with
initCausepreserves diagnostic context; missing this is a common error. - Best Practices — the "never swallow" rule and the checked exception controversy are recurring interview topics.
Related Domains
- Core Java — control flow basics;
throwandthrowsare part of the language syntax. - Spring Boot —
@ExceptionHandlerand@ControllerAdvicebuild on custom exception design. - Testing —
assertThrowsin JUnit 5 requires understanding exception types.