TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C# Coding Standard
Search

Rule:  8@110Checked automatically with code checker

Synopsis:Do not silently ignore exceptions
Language:C#
Severity Level:1
Category:Exceptions


Description:

An empty catch block for all exceptions makes it really hard to find problems. The catch block should at least contain one of the following items:

  • Use the exception argument (e.g. in tracing/logging).
  • Re-throw the exception (either the same one or as inner exception for a more specific one). The re-throw must be preceded by at least one other statement (logging, cleanup, etc).
Some other rules to obey:
  • A simple re-throw only is useless (and can degrade performance) and therefor forbidden.
  • Avoid using "throw e;", since it changes the stack information. A new exception is started from the point of this throw. Just use "throw;" or make a new exception with the caught exception as inner exception.

Examples:

try
{
    ...
} catch (Exception) // <-- Violation 8@110
{   
   // Nothing is done here, so we silently ignore the exception
}

try
{
    ...
} catch (Exception) // <-- Violation 8@110
{   
   // We rethrow the exception, but do nothing extra. This is just extra 
   // overhead and should not be done.
   throw;
}

try
{
    ...
} catch (Exception e) // <-- Violation 8@110
{   
   // We do something here, but nothing with the exception argument.
   i = 0;
   j = 0;
}

try
{
    ...
} catch (Exception e) 
{   
   // This is fine, since we trace the exception
   System.Diagnostics.Trace.WriteLine(e.ToString());
}

try
{
    ...
} catch (Exception e) 
{   
   // This is fine, the original exception is used as inner exception for a more 
   // specific exception.
   throw new MySpecificException(e);
}