Gendarme.Rules.Exceptions.DoNotThrowInUnexpectedLocationRule(git)
Assembly: Gendarme.Rules.Exceptions
Version: git
There are a number of methods which have constraints on the exceptions which they may throw. This rule checks the following methods:
- Property getters - properties should work very much like fields: they should execute very quickly and, in general, should not throw exceptions. However they may throw System.InvalidOperationException, System.NotSupportedException, or an exception derived from these. Indexed getters may also throw System.ArgumentException or System.Collections.Generic.KeyNotFoundException.
- Event accessors - in general events should not throw when adding or removing a handler. However they may throw System.InvalidOperationException, System.NotSupportedException, System.ArgumentException, or an exception derived from these.
- Object.Equals and IEqualityComparer.Equals - should not throw. In particular they should do something sensible when passed null arguments or unexpected types.
- Object.GetHashCode - should not throw or the object will not work properly with dictionaries and hash sets.
- IEqualityComparer.GetHashCode - may throw System.ArgumentException.
- Object.ToString - these are called by the debugger to display objects and are also often used with printf style debugging so they should not change the object's state and should not throw.
- static constructors - should very rarely throw. If they do throw then the type will not be useable within that application domain.
- finalizers - should not throw. If they do (as of .NET 2.0) the process will be torn down.
- IDisposable.Dispose - should not throw. If they do it's much harder to guarantee that objects clean up properly.
- Dispose (bool) - should not throw because that makes it very difficult to clean up objects and because they are often called from a finalizer.
- operator== and operator!= - should not throw. In particular they should do something sensible when passed null arguments or unexpected types.
- implicit cast operators - should not throw. These methods are called implicitly so it tends to be quite surprising if they throw exceptions.
- TryParse methods - should not throw. These methods are designed to be executed without having to catch multiple exceptions (unlike the Parse methods).
Note that the rule does not complain if a method throws System.NotImplementedException because DoNotForgetNotImplementedMethodsRule will flag them. Also the rule may fire with anonymous types with gmcs versions prior to 2.2, see [https://bugzilla.novell.com/show_bug.cgi?id=462622] for more details.
Bad example:
public override bool Equals (object obj)
{
if (obj == null) {
return false;
}
Customer rhs = (Customer) obj; // throws if obj is not a Customer
return name == rhs.name;
}
Good example:
public override bool Equals (object obj)
{
Customer rhs = obj as Customer;
if (rhs == null) {
return false;
}
return name == rhs.name;
}
- This rule is available since Gendarme 2.4
You can browse the latest source code of this rule on github.com
Note that this page was autogenerated (3/17/2011 1:55:44 PM) based on the xmldoc
comments inside the rules source code and cannot be edited from this wiki.
Please report any documentation errors, typos or suggestions to the
Gendarme Mailing List. Thanks!