10

I am looking for a BCL exception type to throw in case of unexpected return value from a call to another method.

I would name it somehow like UnexpectedReturnValueException, but obviously .NET Framework does not have anything like this.

Here is an example of code to illustrate:

public class SomeClass
{
    private int savedMonth;

    private ISomeOtherClass other;

    public void DoSomething(int month, string value)
    {
        if (string.IsNullOrEmpty(value))
        {
            throw new ArgumentNullException("value");
        }

        if (!value.StartsWith("A"))
        {
            throw new ArgumentException("Value should always start with an 'A'", "value");
        }

        if (month < 1 || month > 12)
        {
            throw new ArgumentOutOfRangeException("month", "Month should be in range of 1 to 12");
        }

        if (savedMonth == month)
        {
            throw new InvalidOperationException("Provided month should be different from saved month");
        }

        var result = other.GetResult();

        if (result == null)
        {
            throw new ??? // What should I throw here?
        }

        // do other work here

    }
}

public interface ISomeOtherClass
{
    SomeOtherClassReturnValue GetResult();
}

public class SomeOtherClassReturnValue
{
}

Important:

As per my understanding based on MSDN the following exceptions are not suitable here:

  • ArgumentException - The exception that is thrown when one of the arguments provided to a method is not valid.
  • ArgumentNullException - The exception that is thrown when a null reference is passed to a method that does not accept it as a valid argument.
  • ArgumentOutOfRangeException - The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method.
  • InvalidOperationException - The exception that is thrown when a method call is invalid for the object's current state.
8
  • If other.GetResult() is written in such a way that it can return null, how is that unexpected? Commented May 5, 2014 at 0:55
  • Apparently, this scenario is not common enough to warrant a built-in exception type. You can just create your own exception type and throw that. Commented May 5, 2014 at 0:56
  • @Szymon, as far SomeClass is just using other, it can provide its expectation in form of thrown exception. As an example, let's suppose the other is a Guid generator returning a string. In this case we have all rights to expect the value not to be null. And we express it in code by checking the return value and throwing UnexpectedResultException rather than getting NullReferenceException when using this value or ArgumentNullException when trying to add it to dictionary, for example. Commented May 5, 2014 at 1:25
  • @500-InternalServerError, this is correct, my own exception type is always an option. However I just have a feeling, that there should be something out there which is considered the 'best practice' for this situation :) Commented May 5, 2014 at 1:31
  • 1
    If other.GetResult(); is expected to return null values as a normal behavior, they you should deal with it within the natural flow of the program and not with an Exception. This is also true to your inputs tests. I would recommend having a bool / string return value and not throwing exceptions. You can read more about it on the following link to MSDN msdn.microsoft.com/en-us/library/seyhszts(v=vs.110).aspx Commented May 5, 2014 at 6:46

1 Answer 1

2

If null value is exceptional and therefore exception is needed, best built-in type is InvalidOperationException.

As author states, error is not caused directly from method parameters, therefore no any Argument* -Exception is suitable for this case.

SomeClass has ISomeOtherClass other as a private member in the class and method caller cannot have any visibility to it. ISomeOtherClass-dependency is then part of object's internal state. As documentation states:

InvalidOperationException is thrown when a method call is invalid for the object's current state.

InvalidOperationException is used in cases when the failure to invoke a method is caused by reasons other than invalid arguments.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.