title: Why can't I catch a generic exception in C#?
body: <p>I was doing some unit testing on code that could throw a number of exceptions depending on the inputs. So I tried something like the below code: (simplified for the example)</p>
  
  <pre><code>    static void Main(string[] args)
      {
          RunTest&lt;ArgumentException&gt;();
      }
  
      static void RunTest&lt;T&gt;() where T : Exception, new()
      {
          try
          {
              throw new T();
              //throw new ArgumentException(); &lt;-- Doesn't work either
  
          }
          catch (T tex)
          {
              Console.WriteLine("Caught passed in exception type");
          }
          catch (Exception ex)
          {
              Console.WriteLine("Caught general exception");
          }
          Console.Read();
      }
  </code></pre>
  
  <p>But this will always print out "Caught general exception", the <em>catch(T tex)</em> handler will never work. It doesn't matter whether I throw T() or explicitly throw ArgumentException(). Any ideas why this is? Actually I was kind of surprised that I was even able to use T in the catch clause, but since that's possible shouldn't this work? Or at least give a compiler warning/error that says that this handler will never work?</p>
  
  <p>My environment is Visual Studio 2008 and 3.5 is the target framework.</p>
  
  <p>UPDATE: I tried it now directly from the command prompt and then it prints out "Caught passed in exception type". So it looks like this is restricted to running from within Visual Studio. Maybe a peculiarity of the Visual Studio hosting process?</p>