1

Consider the case where a configuration (a property, in my case) is null.

public Configuration {get;set;}

if (configuration == null)
{
   throw NullReferenceException("Blah blah blah..");
}

But, I read somewhere, "Dont ever throw a null reference exception in your code. NullReferenceException is a runtime exception and should only be raised by the runtime".

If it had been a argument of a function, I thought I would use a ArgumentNullException.

So, what should be the exception in this case? And speaking in general, what exceptions should be thrown at what situations? Googled this but no satisfying answers.

1
  • I mostly prevent situations where I would need a property to be public but may not be null. But, if it is a vital part of the class, I would make it a parameter for the constructor and throw an ArgumentNullException there. If (including that) you still need it to be public I would throw an ArgumentNullException when someone passes null to the setter. However, this might not be the case now and an InvalidOperationException would be best. Commented Apr 17, 2013 at 7:28

5 Answers 5

6

InvalidOperationExceptions states - The exception that is thrown when a method call is invalid for the object's current state ,which isn't a bad fit I suppose? I agree that a null reference isn't what you should throw.

here's another list of common exceptions.

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

3 Comments

Thanks for the list NDJ. What about a ConfigurationErrorsException ?
ConfigurationErrors - 'The exception that is thrown when a configuration-system error has occurred' - I can see where you're coming from, if your property is null because of some configuration error rather than having just not been set.
So, ConfigurationErrors basically means that some error had occurred in the configuration. But, in our case, since the configuration is itself null, we should throw InvalidOperationExceptions ? Is this correct ?
4

The advice is correct, because a null reference exception doesn't say anything about what's actually wrong.

If the value isn't allowed to be null, then you should try to find an exception that describes what's wrong. The problem isn't that the reference is null, but the underlying reason why the reference is null.

If you can't find any exception class that is close enough, you could for example create your own ConfigurationMissingException exception.

Comments

4

InvalidOperationException

The exception that is thrown when a method call is invalid for the object's current state.

Which sounds like your situation.

Basically, if it's not argument related, and you want to throw a built-in exception, your choices usually come down to one of two exceptions. If you'll never be able to honor the request, NotImplementedException is appropriate. But if it's a matter of configuration or state, InvalidOperationException fits the bill.

Comments

3

You should throw no exceptions at what case. It only uses if happens something unexpected like you try set some null value to Configuration. But if Configuration can be null and it already is, you should handle it out in other way.

Comments

2

I think there are actually three cases here:

Firstly, can this happen as a result of the user of the class doing things wrongly? Did they forget to call or set something first (i.e. is there a temporal dependency that they violated)?

If so, then I think the appropriate exception is either an InvalidOperationException, with a Message that describes how to fix the problem, or perhaps you might want to specify a Code Contract as described below.

Secondly, can this only happen due to a logic bug in the class? In other words, should it be impossible for this to happen no matter how the user of the class uses its public methods and properties?

If so, then if you are using Code Contracts you can declare this to be the case by stating:

Contract.Assume(configuration != null);

I find this to be much better. However, the exception thrown by a violation is uncatchable other than if you catch Exception. This is deliberate, and the right choice IMO.

If you're not using code contracts, then you're stuck with throwing InvalidOperationException.

Thirdly, if this exception arises naturally because of external factors outside the program's control, you should probably write a custom exception type for it if there is no existing one which matches the problem. However, for this particular example it seems unlikely that this is the case. I would expect it to be handled elsewhere.

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.