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

TopicDescription
Exception HierarchyThrowable → Error vs. Exception; checked vs. unchecked (RuntimeException).
try / catch / finallyMulti-catch |, finally guarantees, try-with-resources, suppressed exceptions.
Custom ExceptionsCreating domain-specific exceptions; typed fields; hierarchy design; Spring @ControllerAdvice integration.
Exception Best PracticesFail-fast, never swallow, log-once, InterruptedException, and the checked vs. unchecked design decision.

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 the (String, Throwable) constructor preserves the root cause; missing this is the #1 exception anti-pattern.
  4. Exception 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.