期望空值时抛出的exception?
如果我期望一个空值并获得一个定义的值(在一个属性的getter中)并想要抛出exception,那么在csharp中执行此操作的正确方法是什么? 在这种情况下,是否已经定义了哪些有意义?
我的猜测是:
throw new ArgumentException("Parameter was expected to be null, value was provided.");
ArgumentOutOfRangeException
也可能有效,但通常在有明确定义的范围而不是null而不是null时使用。
我可能会使用ArgumentOutOfRangeException
我已经看到使用了InvalidOperationException
,就像在尝试设置两次的上下文中一样。 例如,
if(displayMessage != null) throw new InvalidOperationException("The display message may not be set more than once."); displayMessage = myAwesomeMessage;