Following code should throw exception to prevent adding duplicate collection item.
ICollection<T> collection = new List<T>(); public void Add(T item) { if (collection.Contain(item)) { throw new SomeExceptionType() } collection.Add(item); }
What standard exception type is the most appropriate?
The DuplicateKeyException exception is thrown if an entity EJB object or EJB local object cannot be created because an object with the same key already exists. This exception is thrown by the create methods defined in an entity bean's home or local home interface.
Well, Dictionary<,>.Add()
throws ArgumentException
if such key already exists, so I guess this could be a precedent.
Linq uses two more exceptions DuplicateNameException and DuplicateKeyException you can use these if you are using system.data assembly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With