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,finallyandfinalizesound alike, are unrelated, and get asked together constantly.
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.
Design guidance
- Extend
RuntimeExceptionunless 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
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:
A comment explaining why ignoring is correct is what separates this from the anti-pattern.
printStackTrace
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:
Losing the cause
Always pass it through. This is the most expensive single habit in exception handling.
Exceptions as control flow
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
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.
| Aspect | Anti-pattern | What to do instead |
|---|---|---|
| Empty catch | Failure disappears | Log it, or comment why ignoring is correct |
| e.printStackTrace() | Outside your logging pipeline | log.error(message, args, e) |
| Dropping the cause | Root cause unrecoverable | Pass the original as the cause |
| catch (Exception) | Hides your own bugs | Catch the specific types you handle |
| Exceptions in a loop | Stack capture per iteration | Use the normal condition |
| throws Exception | Tells callers nothing | Declare the specific types |
Empty catch
Anti-patternFailure disappearsWhat to do insteadLog it, or comment why ignoring is correcte.printStackTrace()
Anti-patternOutside your logging pipelineWhat to do insteadlog.error(message, args, e)Dropping the cause
Anti-patternRoot cause unrecoverableWhat to do insteadPass the original as the causecatch (Exception)
Anti-patternHides your own bugsWhat to do insteadCatch the specific types you handleExceptions in a loop
Anti-patternStack capture per iterationWhat to do insteadUse the normal conditionthrows Exception
Anti-patternTells callers nothingWhat 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.