Frage

I know the summaries and descriptions.

But what if the ARGUMENT is in an INVALID STATE?

I think the ArgumentException is more appropriate because the InvalidOperationException documentation says that the object on what the method is called itself has to be in an invalid state. Does this make sense?

War es hilfreich?

Lösung

The documentation says it pretty clearly:

InvalidOperationException is used in cases when the failure to invoke a method is caused by reasons other than invalid arguments.
...
If the method invocation failure is due to invalid arguments, then ArgumentException or one of its derived classes, ArgumentNullException or ArgumentOutOfRangeException, should be thrown instead.

Your method expects the arguments to be in a certain state, which could include anything including being in a "valid state" as defined by the argument type itself.
I think the main point of difference is the source of the problem:
Is it the argument or the object on which you call the method?

Andere Tipps

You probably missed with lack of example.

ArgumentException : Example would be a function end up in an exception because of a invalid argument passed to that function. This kind of exception are handled properly using any defensive mechanism which is ideal in given case.

InvalidOperationException: Example would be modifying a collection while an enumerator is in force.This cause a invalid operation exception.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top