Gendarme.Rules.Exceptions
From Mono
Gendarme's rules about exceptions are located in the Gendarme.Rules.Exceptions.dll assembly. Latest sources are available from anonymous SVN (http://anonsvn.mono-project.com/viewcvs/trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/) (tarball (http://anonsvn.mono-project.com/viewcvs/trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions.tar.gz?view=tar)).
| Table of contents |
Rules
DontSwallowErrorsCatchingNonspecificExceptionsRule
This rule is used for ensure that you don't swallow the errors that you catch. If you decide catch a non-specific exception, you should take care, because you wont know exactly what went wrong. You should catch exceptions when you know why an exception can be thrown, and you can take a decision based on the failure.
Bad example:
try { File.Open ("foo.txt", FileMode.Open); } catch (Exception) { //Ooops what's failed ??? UnauthorizedException, FileNotFoundException ??? }
Good examples:
try { File.Open ("foo.txt", FileMode.Open); } catch (FileNotFoundException exception) { //I know that the system can't find the file. } try { File.Open ("foo.txt", FileMode.Open); } catch { Console.WriteLine ("An error has happened."); throw; // You don't swallow the error, because you rethrow the original exception. }
DontDestroyStackTraceRule
A catch block in the specified method or property is throwing back the caught exception. This will destroy the stack trace of the original exception. If you need to (re-)throw the exception caught by the catch block, you should use throw; instead of throw ex;.
Bad example:
try { Int32.Parse ("Broken!"); } catch (Exception ex) { Assert.IsNotNull (ex); throw ex; }
Good example:
try { Int32.Parse ("Broken!"); } catch (Exception ex) { Assert.IsNotNull (ex); throw; }
Feedback
Please report any documentation errors, typos or suggestions to the Gendarme Google Group (http://groups.google.com/group/gendarme). Thanks!

Powered by MediaWiki