문제

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?

도움이 되었습니까?

해결책

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?

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top