[ACCEPTED]-Which Exception to throw when a method try to use a field that can be null?-null
Throw InvalidOperationException
:
The exception that is thrown when 11 a method call is invalid for the object's 10 current state.
Note that the null reference 9 isn't being passed into the method - it's already 8 there when the method is called - so it's 7 the object's current state which is invalid, not 6 an argument.
However, it would be better 5 to prevent the object from being created 4 in this way to start with, if at all possible 3 - does it have to be a writable property? Would 2 you ever want an instance which did have a 1 null endpoint URI?
NullReferenceException
, InvalidArgumentExecption
or ApplicationException
would all be fine as long as the 2 exception description clearly states what 1 it is that is null.
Like the others I will recommend InvalidOperationException
(Because 2 John Skeet said it) :)
If you call a function 1 with a null parameter then i will use ArgumentNullException
.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.