Which Exception to throw when a method try to use a field that can be null?
NullReferenceException
, InvalidArgumentExecption
or ApplicationException
would all be fine as long as the exception description clearly states what it is that is null.
Throw InvalidOperationException
:
The exception that is thrown when a method call is invalid for the object's current state.
Note that the null reference isn't being passed into the method - it's already there when the method is called - so it's the object's current state which is invalid, not an argument.
However, it would be better to prevent the object from being created in this way to start with, if at all possible - does it have to be a writable property? Would you ever want an instance which did have a null endpoint URI?