title: Best exception for an invalid generic type argument
body: <p>I'm currently writing some code for <a href="http://code.google.com/p/unconstrained-melody/" rel="noreferrer">UnconstrainedMelody</a> which has generic methods to do with enums.</p>
  
  <p>Now, I have a static class with a bunch of methods which are <em>only</em> meant to be used with "flags" enums. I can't add this as a constraint... so it's possible that they'll be called with other enum types too. In that case I'd like to throw an exception, but I'm not sure which one to throw.</p>
  
  <p>Just to make this concrete, if I have something like this:</p>
  
  <pre><code>// Returns a value with all bits set by any values
  public static T GetBitMask&lt;T&gt;() where T : struct, IEnumConstraint
  {
      if (!IsFlags&lt;T&gt;()) // This method doesn't throw
      {
          throw new ???
      }
      // Normal work here
  }
  </code></pre>
  
  <p>What's the best exception to throw? <code>ArgumentException</code> sounds logical, but it's a <em>type</em> argument rather than a normal argument, which could easily confuse things. Should I introduce my own <code>TypeArgumentException</code> class? Use <code>InvalidOperationException</code>? <code>NotSupportedException</code>? Anything else?</p>
  
  <p>I'd <em>rather</em> not create my own exception for this unless it's clearly the right thing to do.</p>