<p>I have an Asset object that has a property AssignedSoftware, which is a collection.  </p>

<p>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. </p>

<p>Is there a standard .NET exception that I should be throwing?  Or does best practices dictate I create my own custom exception?</p>

[{'answer_id': 54800, 'author': 'Kris Erickson', 'author_id': 3798, 'author_profile': 'https://Stackoverflow.com/users/3798', 'pm_score': 3, 'selected': False, 'text': '<p>You should probably throw ArgumentException, as that is what the base library classes do.</p>\n'}, {'answer_id': 54807, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 1, 'selected': False, 'text': '<p>Well, if you really want an collection with unique items, you might want to take a look at the <a href="http://msdn.microsoft.com/en-us/library/bb359438.aspx" rel="nofollow noreferrer">HashSet object</a> (available in C# 3.0).</p>\n\n<p>Otherwise, there are two approaches that you can take:</p>\n\n<ul>\n<li>Create a custom exception for your operation, just as you had stated</li>\n<li>Implement an Add() method that returns a boolean result: true if the item is added and false if the item already has a duplicate in the collection</li>\n</ul>\n\n<p>Either approach can be considered best practice, just as long as you are consistent in its use.</p>\n'}, {'answer_id': 54816, 'author': 'DaveK', 'author_id': 4244, 'author_profile': 'https://Stackoverflow.com/users/4244', 'pm_score': 3, 'selected': False, 'text': '<p>.Net will throw a System.ArgumentException if you try to add an item to a hashtable twice with the same key value, so it doesnt look like there is anything more specific. You may want to write your own exception if you need something more specific.</p>\n'}, {'answer_id': 54817, 'author': 'Adam Wright', 'author_id': 1200, 'author_profile': 'https://Stackoverflow.com/users/1200', 'pm_score': 4, 'selected': True, 'text': '<p>From the Class Library design guidelines for errors (<a href="http://msdn.microsoft.com/en-us/library/8ey5ey87(VS.71).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/8ey5ey87(VS.71).aspx</a>):</p>\n<blockquote>\n<p>In most cases, use the predefined exception types. Only define new exception types for programmatic scenarios, where you expect users of your class library to catch exceptions of this new type and perform a programmatic action based on the exception type itself. This is in lieu of parsing the exception string, which would negatively impact performance and maintenance.</p>\n<p>...</p>\n<p>Throw an ArgumentException or create an exception derived from this class if invalid parameters are passed or detected.</p>\n<p>Throw the InvalidOperationException exception if a call to a property set accessor or method is not appropriate given the object\'s current state.</p>\n</blockquote>\n<p>This seems like an &quot;Object state invalid&quot; scenario to me, so I\'d pick InvalidOperationException over ArgumentException: The parameters are valid, but not at this point in the objects life.</p>\n'}, {'answer_id': 55852, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 4, 'selected': False, 'text': "<p>Why has <code>InvalidOperationException</code> been accepted as the answer?! It should be an <code>ArgumentException</code>?!</p>\n\n<p><code>InvalidOperationException</code> 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 <strong>not the object being Added to, but the object being passed to the object (it's a dupe).</strong> Think about it, if this Add call never took place, would the object still function as normal, YES!</p>\n\n<p>This should be an <strong>ArgumentException</strong>.</p>\n"}]
['https://Stackoverflow.com/questions/54789', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1768/']