Skip to main content
PreparationZone Logo
PrepZone

Designing for Failure

Custom exceptions, what to log, the final/finally/finalize confusion, and anti-patterns to drop.

Pass checkpoint below

Why this matters

  • The difference between a five-minute fix and a five-hour investigation is almost always what was in the exception, and whether anything swallowed it.
  • Custom exceptions let callers handle specific failures instead of parsing error strings, which is the difference between an API and a guess.
  • final, finally and finalize sound alike, are unrelated, and get asked together constantly.
try block starts
No — try completes
Yes — matching catch runs
finally always runsClose resources here
finally runs on every path — success, handled failure, or an exception on the way out. That is why cleanup belongs there, or better still in try-with-resources.

Custom exceptions

Define one when callers need to distinguish your failure from everything else. Give it the fields a caller or a log would need.

Java
Loading compiler…

Design guidance

  • Extend RuntimeException unless the caller has a realistic recovery path. Most application failures do not.
  • Always offer a constructor taking a cause, so wrapping never loses the original.
  • Carry structured data as fields, not only in the message. A caller should not have to parse text to learn the account id.
  • End the name in Exception. This is a universal convention and deviating from it looks like a mistake.
  • Do not create one per method. A family of three or four well-named exceptions per module is plenty.

Anti-patterns to drop

The empty catch block

Java
Loading compiler…

The failure happened, and now nobody will ever know. This turns a clear error into mysterious wrong behaviour somewhere else entirely. If you genuinely expect and can ignore a specific exception, say so:

Java
Loading compiler…

A comment explaining why ignoring is correct is what separates this from the anti-pattern.

printStackTrace

Java
Loading compiler…

It writes to standard error outside your logging framework, so it carries no timestamp, no log level, no request identifier, and will not appear in your log aggregation. Use the logger and pass the exception as the last argument so the framework formats the trace:

Java
Loading compiler…

Losing the cause

Java
Loading compiler…

Always pass it through. This is the most expensive single habit in exception handling.

Exceptions as control flow

Java
Loading compiler…

Constructing an exception captures the stack, which is far more expensive than a comparison. Use the condition that exists for the purpose — while (iterator.hasNext()).

Catching what you cannot handle

Java
Loading compiler…

A NullPointerException from your own mistake now silently returns a default. Catch the specific types you can genuinely act on, and let the rest reach a handler that reports them.

AspectAnti-patternWhat to do instead
Empty catchFailure disappearsLog it, or comment why ignoring is correct
e.printStackTrace()Outside your logging pipelinelog.error(message, args, e)
Dropping the causeRoot cause unrecoverablePass the original as the cause
catch (Exception)Hides your own bugsCatch the specific types you handle
Exceptions in a loopStack capture per iterationUse the normal condition
throws ExceptionTells callers nothingDeclare the specific types
  • Empty catch

    Anti-patternFailure disappears
    What to do insteadLog it, or comment why ignoring is correct
  • e.printStackTrace()

    Anti-patternOutside your logging pipeline
    What to do insteadlog.error(message, args, e)
  • Dropping the cause

    Anti-patternRoot cause unrecoverable
    What to do insteadPass the original as the cause
  • catch (Exception)

    Anti-patternHides your own bugs
    What to do insteadCatch the specific types you handle
  • Exceptions in a loop

    Anti-patternStack capture per iteration
    What to do insteadUse the normal condition
  • throws Exception

    Anti-patternTells callers nothing
    What to do insteadDeclare the specific types

Every one of these makes a failure harder to diagnose later, which is the only cost that matters.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 8 sections

  • What to log, and once
  • Checked exceptions inside lambdas and streams
  • Should you catch OutOfMemoryError or StackOverflowError?
  • The cost of exceptions — not for control flow
  • InterruptedException is special
  • final, finally and finalize
  • A short checklist
  • Common misreadings

Test yourself

Answer these before moving on — recall is what makes it stick.