[ACCEPTED]-Which Exception to throw when a method try to use a field that can be null?-null

Accepted answer
Score: 53

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?

Score: 2

NullReferenceException, InvalidArgumentExecption or ApplicationException would all be fine as long as the 2 exception description clearly states what 1 it is that is null.

Score: 0

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