何时使用InvalidOperationException或NotSupportedException? [英] When to use InvalidOperationException or NotSupportedException?
问题描述
我正在实现一个自定义集合实现,可以是只读的也可以是非只读的;也就是说,所有更改集合的方法都将调用一个在道德上等同于以下功能的函数:
I am implementing a custom collection implementation that can either be readonly or non-readonly; that is, all the methods that change the collection call a function that is the moral equivalent of:
private void ThrowIfReadOnly() {
if (this.isReadOnly)
throw new SomeException("Cannot modify a readonly collection.");
}
我不确定 NotSupportedException哪一个
或 InvalidOperationException
在这种情况下应该使用。
I am not sure which of NotSupportedException
or InvalidOperationException
I should use in that case.
推荐答案
MSDN仅在 NotSupportedException
:
The MSDN only has one bit of guidance on this precise topic, on NotSupportedException
:
对于某些情况下有时对象可能执行请求的操作,并且对象状态确定是否可以执行该操作,请参见
InvalidOperationException
。
以下内容纯属我自己的解释的规则:
What follows is purely my own interpretation of the rule:
- 如果对象的状态可以更改,则操作在对象的'期间可以变为无效/有效的生存期,则应使用
InvalidOperationException
。 - 如果操作在整个对象的生存期内始终是无效/有效的,则
NotSupportedException
。 - 在这种情况下,生命周期的意思是任何人都可以引用该对象的整个时间-也就是说,即使在
Dispose()
调用之后,通常也会使大多数其他实例方法不可用;
- 如Martin Liversage所指出的那样,在对象被处置的情况下,更具体的
ObjectDisposedException
应该使用type。 (这仍然是InvalidOperationException
的子类型。)
- If the object's state can change so that the operation can become invalid / valid during the object's lifetime, then
InvalidOperationException
should be used. - If the operation is always invalid / valid during the whole object's lifetime, then
NotSupportedException
should be used. - In that case, "lifetime" means "the whole time that anyone can get a reference to the object" - that is, even after a
Dispose()
call that often makes most other instance methods unusable;- As pointed out by Martin Liversage, in the case of an object having been disposed, the more specific
ObjectDisposedException
type should be used. (That is still a subtype ofInvalidOperationException
).
在这种情况下,这些规则的实际应用如下:
The practical application of these rules in that case would be as follows:
- 如果
isReadOnly
只能在创建对象时设置(例如,构造函数参数),而不能在任何其他时间设置,然后NotSupportedException
应该使用。 - 如果
isReadOnly
在对象的生存期内可以更改,则InvalidOperationException
应该被使用。
- 但是,
InvalidOperationException
与NotSupportedException $ c $在实现集合的情况下,c>实际上是没有意义的-给定 <$ MSDN上的c $ c> IsReadOnly
,IsReadOnly
唯一允许的行为是,初始化集合后其值永远不会更改。这意味着一个collection实例可以是可修改的也可以是只读的-但它应该在初始化时选择一个实例,并在整个生命周期中坚持使用它。
- If
isReadOnly
can only be set at the time when the object is created (e.g. a constructor argument), and never at any other time, thenNotSupportedException
should be used. - If
isReadOnly
can change during the lifetime of the object, thenInvalidOperationException
should be used.- However, the point of
InvalidOperationException
vsNotSupportedException
is actually moot in the case of implementing a collection - given the description ofIsReadOnly
on MSDN, the only permitted behavior forIsReadOnly
is that its value never changes after the collection is initialized. Meaning that a collection instance can either be modifiable or read-only - but it should choose one at initialization and stick with it for the rest of its lifetime.
这篇关于何时使用InvalidOperationException或NotSupportedException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- However, the point of
- 但是,
- As pointed out by Martin Liversage, in the case of an object having been disposed, the more specific
- 如Martin Liversage所指出的那样,在对象被处置的情况下,更具体的