Skip to main content

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:

TopicDescription
Exception HierarchyThrowable → Error vs. Exception; checked vs. unchecked (RuntimeException).
try / catch / finallyMulti-catch |, finally guarantees, try-with-resources.
Custom ExceptionsCreating domain-specific exceptions; adding context and cause chaining.
Best PracticesFail-fast, don't swallow exceptions, when to use checked vs. unchecked.

Learning Path

  1. Exception Hierarchy — understand the Throwable tree; know that Error should never be caught.
  2. try / catch / finally — learn try-with-resources first since it eliminates most explicit finally blocks.
  3. Custom Exceptions — wrapping causes with initCause preserves diagnostic context; missing this is a common error.
  4. Best Practices — the "never swallow" rule and the checked exception controversy are recurring interview topics.
  • Core Java — control flow basics; throw and throws are part of the language syntax.
  • Spring Boot@ExceptionHandler and @ControllerAdvice build on custom exception design.
  • TestingassertThrows in JUnit 5 requires understanding exception types.