对于未初始化的集合最合适的例外?
当您的类必须使用至少一项初始化其集合属性时,最合适的 .Net 异常类型是什么?
我认为这将是一个 ArgumentOutOfRangeException
但基于集合是否有更合适的东西?
What is the most appropriate .Net exception type for when you have a class which must have its collection property initialised with at least one item?
I'm thinking that it would be an ArgumentOutOfRangeException
but is there something more appropriate based on a collection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为示例,您可以查看
System.Linq.Queryable.Single
方法,它是IQueryable
接口的扩展方法,并抛出InvalidOperationException
> 如果集合中有多个元素。恕我直言,
InvalidOperationException
是比ArgumentOutOfRangeException
更糟糕的选择,但我认为由于 Microsoft 使用了InvalidOperationException
,.NET 中似乎没有相关的异常类。As an example, you can have a look at
System.Linq.Queryable.Single
method, which is an extension method toIQueryable
interface and throwsInvalidOperationException
in case there are more than one elements in the collection.IMHO,
InvalidOperationException
is worse choice thanArgumentOutOfRangeException
, but I assume since Microsoft has usedInvalidOperationException
, it seems there is no relevant exception class in .NET.您始终可以创建自己的
MyCollectionNotInitialized
异常。我认为这比使用任何不合适的异常要好。You can always create your own
MyCollectionNotInitialized
exception. I think it is better than using any not suitable exception.