Gendarme.Rules.Exceptions

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)).


Rules

AvoidArgumentExceptionDefaultConstructorRule

This rule check that any System.ArgumentException , System.ArgumentNullException , System.ArgumentOutOfRangeException or System.DuplicateWaitObjectException exception created are provided with some useful information about the exception being throw, minimally the parameter name.

Bad example:

public void Add (object obj)
{
    if (obj == null)
    throw new ArgumentNullException ();
    Inner.Add (obj);
}

Good example:

public void Add (object obj)
{
    if (obj == null)
    throw new ArgumentNullException ("obj");
    Inner.Add (obj);
}

Notes

  • This rule is available since Gendarme 2.0

AvoidThrowingBasicExceptionsRule

This rule check for methods that create basic exceptions like System.Exception , System.ApplicationException or System.SystemException . Those exceptions do not provide enough information about an error condition to be helpful to the consumer of the library.

Bad example:

public void Add (object obj)
{
    if (obj == null)
    throw new Exception ();
    Inner.Add (obj);
}

Good example:

public void Add (object obj)
{
    if (obj == null)
    throw new ArgumentNullException ("obj");
    Inner.Add (obj);
}

Notes

  • This rule is available since Gendarme 2.0

DoNotDestroyStackTraceRule

This rule check method's catch block to see if they are throwing back the caught exception. Doing so would 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;
}

Notes

  • Prior to Gendarme 2.0 this rule was named DontDestroyStackTraceRule.

DoNotSwallowErrorsCatchingNonSpecificExceptionsRule

This rule is used for ensure that methods do not swallow the catched exceptions. 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 example (catch a specific exception):

try {
    File.Open ("foo.txt", FileMode.Open);
}
catch (FileNotFoundException exception) {
    //I know that the system can't find the file.
}

Good example (catch all and rethrow):

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.
}

Notes

  • Prior to Gendarme 2.0 this rule was named DontSwallowErrorsCatchingNonspecificExceptionsRule.

DoNotThrowReservedExceptionRule

This rule check for methods that create reserved exceptions like System.ExecutionEngineException , System.IndexOutOfRangeException , NullReferenceException or System.OutOfMemoryException . Those exceptions should only be thrown by the .NET runtime.

Bad example:

public void Add (object obj)
{
    if (obj == null)
    throw new NullReferenceException ("obj");
    Inner.Add (obj);
}

Good example:

public void Add (object obj)
{
    if (obj == null)
    throw new ArgumentNullException ("obj");
    Inner.Add (obj);
}

Notes

  • This rule is available since Gendarme 2.0

ExceptionShouldBeVisibleRule

This rule check for non visible exception types that derive directly from the most basic exceptions: System.Exception , System.ApplicationException or System.SystemException . Those basic exceptions, being visible, will be the only information available to the API consumer - but do not contain enough data to be useful.

Bad example:

internal class GeneralException : Exception {
}

Good example (visibility):

public class GeneralException : Exception {
}

Good example (base class):

internal class GeneralException : ArgumentException {
}

Notes

  • This rule is available since Gendarme 2.0

InstantiateArgumentExceptionCorrectlyRule

This rule check that any System.ArgumentException , System.ArgumentNullException , System.ArgumentOutOfRangeException or System.DuplicateWaitObjectException exception created to ensure the order of their parameters, in particular the position of parameterName , is correct. This is a common mistake since the position is not consistent across all exceptions.

Bad example:

public void Show (string s)
{
    if (s == null)
    throw new ArgumentNullException ("string is null", "s");
    if (s.Length == 0)
    return new ArgumentException ("s", "string is empty");
    Console.WriteLine (s);
}

Good example:

public void Show (string s)
{
    if (s == null)
    throw new ArgumentNullException ("s", "string is null");
    if (s.Length == 0)
    return new ArgumentException ("string is empty", "s");
    Console.WriteLine (s);
}

Notes

  • This rule is available since Gendarme 2.2

MissingExceptionConstructorsRule

This rule check that any exceptions, i.e. types deriving from System.Exception , provide all the necessary constructors that can be expected from consumer or required by the runtime.

Bad example:

public class GeneralException : Exception {
    // it should be a default public constructor
    private GeneralException ()
    {
    }
}

Good example:

public class GeneralException : Exception {
    public GeneralException ()
    {
    }
    
    public GeneralException (string message)
    {
    }
    
    public GeneralException (string message, Exception inner)
    {
    }
    
    protected GeneralException (SerializationInfo info, StreamingContext context)
    {
    }
}

Notes

  • This rule is available since Gendarme 2.0

Feedback

Please report any documentation errors, typos or suggestions to the Gendarme Google Group (http://groups.google.com/group/gendarme). Thanks!