InvalidOperationException vs. ArgumentException [closed]
Asked Answered
S

2

14

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?

Sevilla answered 8/5, 2013 at 7:18 Comment(1)
Too localized... Actual users don't care about that... It is important what you make out of it and so it is pure taste...Pyrrhic
C
18

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?

Classieclassification answered 8/5, 2013 at 7:26 Comment(0)
I
2

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.

Inviolable answered 8/5, 2013 at 7:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.