c# - InvalidOperationException 与 ArgumentException

标签 c# invalidoperationexception argumentexception

<分区>

我知道摘要和描述。

但是如果 ARGUMENT 处于无效状态怎么办?

我认为 ArgumentException 更合适,因为 InvalidOperationException 文档说调用方法的对象本身必须处于无效状态。这有意义吗?

最佳答案

文档说的很清楚:

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.

您的方法期望参数处于特定状态,该状态可以包括任何状态,包括处于由参数类型本身定义的“有效状态”。
我认为主要的区别在于问题的根源:
是调用方法的参数还是对象?

关于c# - InvalidOperationException 与 ArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16434842/

相关文章:

c# - 无需签名即可部署 ClickOnce 应用程序

c# - 如果返回类型是可枚举的,返回列表是否错误

c# - 使用 Unity 如何将命名依赖项注入(inject)构造函数?

.net - Python 中 .Net InvalidOperationException 的类比是什么?

c# - ArgumentException:C# 中的 'Illegal characters in path'

c# - 如果验证方法应该返回验证结果,为什么会有 ArgumentException 类?

c# - 如何使用 visual studio 将某些行排除在构建之外?

asp.net - InvalidOperationException:不存在数据时尝试读取无效。 (SQL)

c#: "System.InvalidOperationException: Queue empty"和使用 SendKey.SendWait() 时的其他错误

c# - 为什么在 Xamarin 中将最小 100 放在步进器类上不起作用?