title: Unhandled Exception handling in Winforms
body: <pre><code>[STAThread]
      static void Main()
      {
          try
          {
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
              Application.ThreadException +=
                  new ThreadExceptionEventHandler(Application_ThreadException);
              Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
              AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
              Application.Run(new Form1());                
          }
          catch (Exception e)
          {                
  
          }
      }
  static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  {
     //Code
  }
  static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
  {
     //Code
  }
  </code></pre>
  
  <p>In Form1.cs I have the following.</p>
  
  <pre><code>private void backgroundWorkerLogin_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
  {
       throw new Exception("Test");
  }
  </code></pre>
  
  <p>Still the exception is shown as unhandled. Can anyone suggest how to catch the exception by the event handler declared above?</p>
  
  <p>Thanks.</p>