The Error Handling Spectrum: Let It Crash to Defensive
What you'll learn: You'll understand the range of error handling strategies from fail-fast "let it crash" approaches to defensive programming, and when each makes sense.
The Spectrum Explained
Error handling isn't one-size-fits-all. Think of it as a spectrum between two extremes:
"Let it crash" (fail-fast): When an error occurs, immediately stop execution and propagate the failure upward. No second-guessing, no recovery attempts at the point of failure. This philosophy trusts that supervisors or higher-level components will handle cleanup and recovery.
Defensive programming: Anticipate every possible error, validate all inputs, check all preconditions, and attempt recovery or graceful degradation wherever possible. Handle errors close to where they occur.
When to Choose Each Approach
Let It Crash
Use when:
- You have a supervision system that can restart failed components
- Continuing with corrupted state is more dangerous than stopping
- The error indicates a programmer mistake (bug), not a recoverable condition
- Quick failure and restart is cheaper than complex recovery logic
Example: Erlang/Elixir systems where supervisor processes automatically restart crashed workers with clean state.
Defensive Programming
Use when:
- You're at system boundaries (parsing user input, calling external APIs)
- Recovery is possible and makes sense
- The cost of failure is very high (medical devices, financial transactions)
- You can provide meaningful fallbacks or degraded functionality
Example: Validating every API request parameter before processing to provide clear error messages rather than crashing.
The Middle Ground
Most systems use both strategies at different layers. Validate defensively at boundaries, then let internal components fail fast when they receive invalid data that "shouldn't" be possible.
Key Takeaway: Choose fail-fast when clean failure is better than uncertain state; choose defensive programming at boundaries and when graceful degradation is possible. Most robust systems use both strategically.