0

I have asked myself this many times, but until this day I have not found a good answer. What is the correct exception to throw, if you realize there is an invalid state in the argument that has passed to your method, e.g. if I need to make sure a certain field is set inside the parameter. Take a look at the following example:

public void DoSomethingElse(SomeObject parameter) 
{
    if (parameter.Field is null)
    {
        throw new ArgumentNullException(); // what to throw here?
    }
}

What kind of exception should I throw here? It's not an ArgumentNullException, because the Argument is not null, but it's field. Also FxCop gives you a CA2208. I was thinking about NotSupportedException or InvalidOperationException, but both are mainly for other use cases and no good fits. I normally tend to throw a plain ArgumentException, but it's not as descriptive as the ArgumentNullException, thus I have to provide a more meaningful explanation inside the exception message.

Another example is the following switch statement.

public void DoSomething(SomeObject parameter) 
{
   switch(parameter.SomeEnum) 
   {
       case SomeEnum.Value1:
           // Do Something
           break; 
       case SomeEnum.Value2:
           // Do Something
           break; 
       default:
           throw new ArgumentOutOfRangeException(); // what to throw here?
   }
}

Again, the argument itself is not out of range, but its field. Again, the ArgumentOutOfRangeException is kind of the best match, but it's still not the argument, that's a problem. In the case of a switch, the IndexOutOfRangeException seems like a good fit on first sight, but we're not using an index. Hence I again fall back to the ArgumentException and feel the need to describe the problem in the exception message.

So my question is, what's the best way to handle cases like the above, where not the argument itself but the its contents are in an invalid state?

crazy_crank
  • 659
  • 4
  • 17
  • 1
    I think Argument exception is valid,you just need to pass a message into the exception ctor to add some clarity. You could create some utility methods for doing this potentially taking some values such as argument name and property name and then it can do the deed of building the message and constructing your exception. If you really, really dont like any of the exceptions... you could create you're own, but then is the reader of your exception going to understand what your custom exception mean? – Dave Nov 09 '18 at 14:15
  • If you're not happy with any of the exceptions remember that you can create your own. – Paul Karam Nov 09 '18 at 14:51

1 Answers1

1

I think this is where you'd use the parameters of the exception to provide extra detail.

if (parameter.Field is null)
{
    throw new ArgumentException("Field is null", "parameter"); // what to throw here?
}

Or change the function to take just that parameter, not the whole object. Then ArgumentNullException would be appropriate.

Robin Bennett
  • 3,192
  • 1
  • 8
  • 18