title: Casting Exceptions in C#
body: <p>Why do I get an <code>InvalidCastException</code> when trying to do this?</p>
  
  <pre><code>throw (ArgumentNullException)(new Exception("errormessage", null));
  </code></pre>
  
  <p>This is a simplified version of the following function.</p>
  
  <pre><code>public static void Require&lt;T&gt;(bool assertion, string message, Exception innerException) where T: Exception
      {
          if (!assertion)
          {
              throw (T)(new Exception(message, innerException));
          }
      }
  </code></pre>
  
  <p>The complete error message is:</p>
  
  <blockquote>
    <p><em>System.InvalidCastException : Unable to cast object of type 'System.Exception' to type 'System.ArgumentNullException'.</em></p>
  </blockquote>