title: Unhandled Exception in Winforms Application
body: <p>I have a simple WinForms app that is used to enter test cases. Ever since I upgraded this application to .NET 4.0 and added a new tab page to the tab page control for validating XML against XSD schema the application has been randomly crashing. I've been unable to reproduce the exception.</p>
  
  <p>The error my QA guy receives is the generic Windows message:</p>
  
  <p>TestCaseViewer has encountered a problem and needs to close. We are sorry for the inconvenience.</p>
  
  <p>To try to get to the real error I've added the following code to the beginning of the Main method of program:</p>
  
  <pre><code>        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
          Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
          Application.ThreadException += Application_ThreadException;
  </code></pre>
  
  <p>The event handlers look like this:</p>
  
  <pre><code>    static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
      {
          try
          {
              MessageBox.Show(e.Exception.ToString(), @"Thread Exception", 
                  MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
          }
          finally 
          {
              Application.Exit();    
          }
      }
  
      static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
      {
          try
          {
              var ex = (Exception)e.ExceptionObject;
              MessageBox.Show(ex.ToString(), @"Unhandled Exception",
                  MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
          }
          finally 
          {
              Application.Exit();    
          }
      }
  </code></pre>
  
  <p>Unfortunately this hasn't helped and whatever is thowing the error continues to do so in a way that generates the unhandled error that is bubbling up to the OS.</p>
  
  <p>Can anyone give me any other ideas about trapping this exception?</p>