title: Preventing Unhandled Exception Dialog Appearing
body: <p>First let me say I have read <a href="https://stackoverflow.com/questions/219594/net-whats-the-best-way-to-implement-a-catch-all-exceptions-handler">this useful article</a> thoroughly and am using the SafeThread class from CodeProject. I get the same result whether using Thread or SafeThread.</p>
  
  <p>I have reduced my problem down to an app consisting of two forms each with one button. The main program displays a form. When you click that button, a new thread starts which displays a second form. When you click the button on the second form, internally it just does "throw new Exception()"</p>
  
  <p>When I run this under VS2008, I see "Exception in DoRun()". </p>
  
  <p>When I run outside of VS2008, I get a dialog box "Unhandled exception has occurred in your application. If you click continue, the application ...."</p>
  
  <p>I have tried setting legacyUnhandledExceptionPolicy in the app.config to both 1 and 0.</p>
  
  <p>What do I need to do to capture the exception thrown in my second form, when not running under VS2008?</p>
  
  <p>Here's my Program.cs</p>
  
  <pre><code>    static class Program
      {
          [STAThread]
          static void Main()
          {
              Application.ThreadException += new ThreadExceptionEventHandler    (Application_ThreadException);
              Application.SetUnhandledExceptionMode    (UnhandledExceptionMode.CatchException);
              AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
              try
              {
                 Application.Run(new Form1());
              }
              catch(Exception ex)
              {
                  MessageBox.Show("Main exception");
              }                
          }
  
          static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
          {
              MessageBox.Show("CurrentDomain_UnhandledException");
          }
  
          static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
          {
              MessageBox.Show("Application_ThreadException");
          }
      }
  </code></pre>
  
  <p>Here's Form1:</p>
  
  <pre><code>public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          SafeThread t = new SafeThread(new SimpleDelegate(ThreadMain));
          try
          {
              t.ShouldReportThreadAbort = true;
              t.ThreadException += new ThreadThrewExceptionHandler(t_ThreadException);
              t.ThreadCompleted += new ThreadCompletedHandler(t_ThreadCompleted);
              t.Start();
          }
          catch(Exception ex)
          {
              MessageBox.Show(string.Format("Caught externally! {0}", ex.Message));
  
          }
      }
  
      void t_ThreadCompleted(SafeThread thrd, bool hadException, Exception ex)
      {
          MessageBox.Show("t_ThreadCompleted");
      }
  
      void t_ThreadException(SafeThread thrd, Exception ex)
      {
          MessageBox.Show(string.Format("Caught in safe thread! {0}", ex.Message));
      }
  
      void ThreadMain()
      {
          try
          {
              DoRun();
          }
          catch (Exception ex)
          {
              MessageBox.Show(string.Format("Caught! {0}", ex.Message));
          }
      }
  
      private void DoRun()
      {
          try
          {
              Form2 f = new Form2();
              f.Show();
              while (!f.IsClosed)
              {
                  Thread.Sleep(1);
                  Application.DoEvents();
              }
          }
          catch(Exception ex)
          {
              MessageBox.Show("Exception in DoRun()");
          }
      }
  }
  </code></pre>
  
  <p>And here is Form2:</p>
  
  <pre><code>public partial class Form2 : Form
  {
      public bool IsClosed { get; private set; }
  
      public Form2()
      {
          InitializeComponent();
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          throw new Exception("INTERNAL EXCEPTION");
      }
  
      protected override void OnClosed(EventArgs e)
      {
          IsClosed = true;
      }
  }
  </code></pre>