Improper Error Handling
Empty catch blocks, swallowing exceptions.
“Error handling is important, but if it obscures logic, it’s wrong.” – Robert C. Martin
Signs of Use (Symptoms)
-
catch (e) {}- Empty catch blocks. -
catch (e) { console.log(e); }- Logging but not handling. - Passing error codes instead of throwing exceptions.
Why it is bad (Consequences)
- Silent Failures: Bugs go unnoticed.
- Debugging Hell: No stack trace; issues manifest later.
Why it happens (Root Cause)
“The code throws errors, and I don’t know what to do. I’ll just ignore them for now.”
When it might be okay (Exceptions)
- Truly ignorable errors (e.g., closing a stream that might already be closed).
Explanation
Problem
An exception is thrown, caught, and swallowed. The calling code thinks everything worked.
The Flaw
Errors exist for a reason. Ignoring them causes undefined behavior.
Real world analogy
A smoke detector with its battery removed to stop the beeping.
Refactoring Solution
- Handle or rethrow: Either deal with the error or let it propagate.
- Use Exceptions, not error codes.
Pros and Cons (of the Antipattern)
| Pros (Why people do it) | Cons (The price you pay) |
|---|---|
Comparison
- Related Antipatterns: None directly.
- Related Principles: Fail Fast.
Code example
Typescript
Bad (The Antipattern)
Good (The Fix)