ArticlesProjectsWeeklyCredentialsAbout

Java Exception Handling Examples (1998)

Runnable Java examples of correct and incorrect exception handling patterns — swallowing, catching too broadly, translation at boundaries, and the finally cleanup idiom.

javaexception-handlingpatterns

Java Exception Handling Examples (1998)

Side-by-side correct and incorrect exception handling patterns — the kind of examples we used in code review feedback and team training.

Contents

  • AntiPatterns.java — swallowing, catching Exception, empty catch blocks — all demonstrated
  • Patterns.java — specific catching, translation at boundaries, finally cleanup, logging with context
  • DeviceRepository.java — realistic example: JDBC layer translating SQLException to domain exceptions
  • TransactionDemo.java — correct commit/rollback with finally
Source code
# Java Exception Handling Examples (1998)

```sh
mkdir -p build
find src -name '*.java' | xargs javac -d build
```

`AntiPatterns.java` — three common mistakes, annotated.
`Patterns.java` — three correct approaches with the same scenarios.