🔧 catch (Exception) Hides the Bug It Was Supposed to Catch
A broad catch (Exception ex) around a risky block feels safe — nothing crashes the app. But it also catches a NullReferenceException from an unrelated typo three lines above, silently swallowing a real bug alongside the one exception you actually meant to handle.
🐞 The Problem
try
{
var user = _repository.GetUser(userId); // could be null - typo bug
var email = user.Email; // NullReferenceException here
SendWelcomeEmail(email);
}
catch (Exception ex)
{
_logger.LogWarning("Failed to send welcome email, continuing.");
// The NullReferenceException - a real bug - gets the SAME
// "continuing, no big deal" treatment as a genuinely expected
// SmtpException from the mail server being briefly down.
🔍 Why This Happens
catch (Exception) matches every exception type - the ones you anticipated (network timeout, validation failure) AND the ones that indicate an actual bug in your code (null reference, index out of range, invalid cast). Treating both categories the same way means real bugs get logged as routine warnings instead of surfacing as the code defects they are.
✅ The Fix: Catch the Specific Exception You Expect
- Only catch the exception type(s) you actually know how to handle meaningfully.
- Let everything else propagate — an unhandled exception with a full stack trace is far more useful for finding a real bug than a silently swallowed one.
🎯 Correct Version
try
{
var user = _repository.GetUser(userId);
var email = user.Email;
SendWelcomeEmail(email);
}
catch (SmtpException ex)
{
// This is the ONE failure mode we actually expect and can
// meaningfully handle - the mail server being briefly unreachable.
_logger.LogWarning(ex, "Welcome email failed, will retry later.");
_retryQueue.Enqueue(userId);
}
// NullReferenceException from the typo bug above now propagates
// and shows up loudly - exactly where it should.
⚠️ If You Truly Need a Catch-All
- Put it at the highest level of your app (global exception handler / middleware) for logging and a graceful user-facing error — not scattered through business logic where it hides root causes.
- Never catch (Exception) and then do nothing (empty catch block) — that’s the single worst version of this pattern, actively destroying the evidence of a bug.
A specific catch clause is a promise: “I understand exactly this failure mode and know what to do about it” — catch (Exception) breaks that promise and quietly recruits every future bug into looking like routine, handled behavior.


