Empty Catch Block
ID |
csharp.empty_catch_block |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
CSharp |
Tags |
CWE:1069, exception-handling, reliability |
Description
Reports catch clauses whose body has no executable statements. An empty catch silently
swallows the exception, hiding bugs and leaving the program running in an unexpected state
with no log, no rethrow, and no handling.
Rationale
Exceptions exist to signal that something went wrong. A catch block that does nothing
discards that signal: the failure leaves no trace, the original stack is lost, and any
subsequent code runs as if the operation had succeeded. Bugs that should be loud and
reproducible become silent and intermittent.
Comments do not count as handling. A block with only a comment is still empty.
public class Worker
{
public void Broken()
{
try
{
Run();
}
catch (Exception) // FLAW — exception is silently swallowed
{
}
}
public void BrokenCommentOnly()
{
try
{
Run();
}
catch (Exception) // FLAW — comment is not a handler
{
// ignore on purpose
}
}
public void Ok()
{
try
{
Run();
}
catch (Exception ex) // OK — failure is logged
{
Log(ex);
}
}
}
Remediation
Decide how the failure should be handled: log it, wrap it in a domain exception, rethrow it, or use a more specific exception type. If the exception genuinely must be ignored, add a statement that documents and enforces that decision (for example a logger call recording the expected scenario), not just a comment.