开发者

Most appropriate exception for a collection not being initialised?

What is the most appropriate .Net exception type f开发者_StackOverflow中文版or 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?


You can always create your own MyCollectionNotInitialized exception. I think it is better than using any not suitable exception.


As an example, you can have a look at System.Linq.Queryable.Single method, which is an extension method to IQueryable interface and throws InvalidOperationException in case there are more than one elements in the collection. IMHO, InvalidOperationException is worse choice than ArgumentOutOfRangeException, but I assume since Microsoft has used InvalidOperationException, it seems there is no relevant exception class in .NET.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜