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?
Remarks. ArgumentException is thrown when a method is invoked and at least one of the passed arguments does not meet the parameter specification of the called method. The ParamName property identifies the invalid argument.
InvalidOperationException is used in cases when the failure to invoke a method is caused by reasons other than invalid arguments. Typically, it is thrown when the state of an object cannot support the method call. For example, an InvalidOperationException exception is thrown by methods such as: IEnumerator.
The ArgumentOutOfRangeException exception is thrown when the argument passed to a method is not null and contains a value that is not within the set of expected values. This exception type has the properties ParamName and ActualValue which helps understand the cause of the exception.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With