I have seen the following exception case several times
public SomeClass(IEnumerable<T> someValues)
{
if (null == someValues)
{
throw new ArgumentNullException("someValues");
}
int counter = 0;
foreach (T value in someValues)
{
if (null == value)
{
string msg = counter + "th value was null";
// What exception class to use?
throw new ArgumentException(msg, "someValues");
}
counter++;
}
}
Is there a guideline for handling these cases? And in general is there any guidelines describing “exception style” a bit more detailed than the MSDN documentation for
Yes there is, as described here, you should throw framework exceptions when possible and throwing the most derived exception that is applicable. In this case
ArgumentNullExceptionis thrown because an argument is null andArgumentExceptionis thrown because the contents of the enumeration are not directly a parameter of the function, so what you can say is that the argument is invalid, but it is not because it i null or is out of range.If knowing why it is invalid becomes absolutely necessary you could derive from
Argument Exception; something likeArgumentCollectionContainsNullException.