views:

43

answers:

2

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?

+1  A: 

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.

Vitaliy Liptchinsky
+3  A: 

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

Andrew Bezzub
Thanks, I should have mentioned that I had already considered writing my own custom exception but I'm specifically looking for the most appropriate standard .Net exception type. +1 for suggestion though.
Brian Scott
+1 for creating your own exception. This sounds pretty specific to your app so creating your own exception would be the way I would go.
Patrick Steele