Opening Scenario
You're adding a charge_customer(customer_id, amount) function to a billing service. Inside, it will:
- Look up the customer
- Validate the amount is positive and under their limit
- Call the payment gateway (network request, can time out)
- Write a ledger row (DB write, can conflict)
- Send a receipt email (best effort)
Each step can fail in its own way. Now decisions pile up. Does charge_customer throw on a network timeout, or return a Result? Does an invalid amount raise the same type of error as a gateway decline? Should the email failure bubble up and abort the whole flow, or be swallowed? Who turns these into HTTP 400 vs. 500 vs. 402?
A junior on the team just wrote:
# Python
try:
charge_customer(cid, amt)
except Exception:
pass # we'll fix this laterTwo weeks later a customer is double-charged and nobody noticed. This is what an error handling strategy is for — so the answer isn't reinvented case by case.
In the full tutorial
- Question Progression
- When to Reach for This