Patterns / Swallowed exceptions
Swallowed exceptions
Agents are rewarded for code that runs without crashing, so they wrap risky calls in try/catch and move on. The result looks defensive and robust. In practice it hides failures: the function returns undefined, the job reports success, and you find out from a customer.
What it looks like
catch (e) {}
catch (err) { console.error("Something went wrong"); }
catch (e) { throw e; }
except:
pass
except Exception:
pass
Why it matters
Real error handling depends on what can fail and what the caller needs. A model doesn't know that, so it writes the generic version: catch everything, log a vague string, continue.
Before and after
Before
try {
await db.query(sql, [id]);
} catch (e) {
console.error("Something went wrong");
}After
try {
await db.query(sql, [id]);
} catch (err) {
logger.error({ err, id }, "markPaid failed");
throw new Error(`could not mark invoice ${id} paid`, { cause: err });
}How to fix it
- Decide what the caller should see: rethrow with context, return a typed error, or retry.
- If ignoring really is right (best-effort cleanup, optional telemetry), say why in a comment inside the catch. SlopScore for Code doesn't flag documented empty catches.
- Catch the specific exception type in Python, never bare except.
When it's fine
Best-effort operations where failure truly doesn't matter, with a comment saying so.
Catch this automatically. SlopScore for Code checks every pull request for this pattern (rule
swallowed-exception) and 11 others. Score a public PR or add the free GitHub Action.