I have an Asset object that has a property AssignedSoftware, which is a collection.
I want to make sure that the same piece of Software is not assigned to an Asset more than once. In Add method I check to see if the Software already exist, and if it does, I want to throw an exception.
Is there a standard .NET exception that I should be throwing? Or does best practices dictate I create my own custom exception?
Why has InvalidOperationException
been accepted as the answer?! It should be an ArgumentException
?!
InvalidOperationException
should be used if the object having the method/property called against it is not able to cope with the request due to uninit'ed state etc. The problem here is not the object being Added to, but the object being passed to the object (it's a dupe). Think about it, if this Add call never took place, would the object still function as normal, YES!
This should be an ArgumentException.
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