title: Is there a pre-defined ThisIsABugException?
body: <p>The .NET Framework has a lot of pre-defined exceptions. For example, if an invalid parameter was passed to my method, I'm supposed to raise an <code>ArgumentException</code> (or <code>ArgumentNullException</code>, if the paramter was <code>null</code>). If a method is called that is invalid for the current object state, I'm supposed to raise an <code>InvalidOperationException</code>.</p>
  
  <p>Is there some pre-defined exception for the "<em>this code should never be reached</em>" case? Here's an example:</p>
  
  <pre><code>void myMethod() {
      int a;
  
      ... /* Some complex code that manipulates a. In the end, "a" can only be 1 or 2. */
  
      switch (a) {
          case 1: ...
          case 2: ...
          default:  // just an extra sanity check
              // Oops, we should never be here.
              // There's apparently some bug in the code above.
              throw new ThereIsABugInTheCodeException();
      }
  }
  </code></pre>
  
  <p>I don't think creating my own exception for such a rare case is justified. On the other hand, just throwing <code>Exception</code> <a href="http://msdn.microsoft.com/en-us/library/ms182338.aspx" rel="nofollow">is discouraged as well</a>. I guess some kind of <code>AssertionFailedException</code> would be appropriate, but I did not find any in the .net Framework. (Note that <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.trace.assert.aspx" rel="nofollow">Trace.Assert</a> does not throw an exception, so that's not an option either.)</p>
  
  <hr>
  
  <p><strong>EDIT</strong>: To clarify: <code>a</code> is NOT an argument. Let me rephrase the question to make it clearer: In the middle of a method I do a sanity check (just to make sure, it should not be necessary if the code in the beginning of the method is correct). I find out the sanity check fails and would like to throw an exception. Which one do I throw? I don't think that <code>ArgumentException</code> is the correct answer, since <a href="http://blogs.msdn.com/b/codeanalysis/archive/2007/01/22/faq-what-exception-should-i-throw-instead-of-the-reserved-exceptions-found-by-donotraisereservedexceptiontypes.aspx" rel="nofollow"><code>ArgumentException</code> should only be thrown if the <em>caller</em> of the method did something wrong</a>. That is not the case here. The <em>method itself</em> realizes at runtime that it contains a bug.</p>