title: exception raised in BackgroundWorker.RunWorkerCompleted are not visible in Application.ThreadException handler
body: <p>I'm using background worker and found that new exceptions raised in the RunWorkerCompleted event handler are not visible in the Application.ThreadException handler. For example:
  somewhere in the Program.cs:</p>
  
  <pre><code>    [STAThread]
      static void Main()
      {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
  
          Application.ThreadException += delegate(object o, ThreadExceptionEventArgs args)
          {
              if (args.Exception != null)
                  Console.WriteLine("thread error {0}", args.Exception.Message);
          };
  
          Application.Run(new Form1());
      }
  </code></pre>
  
  <p>and then in the button click (for testing purposes only)</p>
  
  <pre><code>        var bw = new BackgroundWorker();
          bw.DoWork += delegate(object o, DoWorkEventArgs args) { /* some calculations here */ };
          bw.RunWorkerCompleted += delegate(object sender1, RunWorkerCompletedEventArgs eventArgs)
          { 
              try
              {
                  //in run worker completed exception occurs
                  throw new ApplicationException("test exception 1");
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message);
                  Console.WriteLine("throwing new exception");
                  throw new ApplicationException(string.Format("catched '{0}'", ex.Message), ex);
              }
          };
  
          bw.RunWorkerAsync();
  </code></pre>
  
  <p>Program output is below:</p>
  
  <p><em>test exception 1</em></p>
  
  <p><em>throwing new exception</em> </p>
  
  <p><em>thread error test exception 1</em></p>
  
  <p>I beleive that in the Application.ThreadException handler exception with text <em>thread error catched 'test exception 1'</em> should appear, not original exception.</p>
  
  <p>Could you please help me - why it happens and how it can be solved? This is only sample code. 
  There are several nested methods inside RunWorkerCompleted handler in the real code. And some of those methods catch exception, and throw new with additional information and original exception in the inner exception. So in the Application.ThreadException handler I would like to get not only original exception that was thrown, but the whole chain.</p>
  
  <p>Thank you</p>
  
  <hr>
  
  <p>I've found that GetBaseException is not called for unhandled exceptions from button click (for example)</p>
  
  <p>lets assume that there is TreadException event handler in the Program.Main method like below:</p>
  
  <pre><code>        Application.ThreadException += (o, args) =&gt;
          {
              Console.WriteLine("BEGIN ThreadException handler");
              ShowException(args.Exception);
              Console.WriteLine("END ThreadException handler\r\n");
          };
  </code></pre>
  
  <p>and Program.ShowException method like this:</p>
  
  <pre><code>    static void ShowException(Exception ex)
      {
          var tab = "";
          while (ex != null)
          {
              Console.WriteLine("{1}error '{0}'", ex.Message, tab);
              tab += '\t';
              ex = ex.InnerException;
          }
      }
  </code></pre>
  
  <p>When code </p>
  
  <pre><code>        try
          {
              throw new ApplicationException("button click 1");
          }
          catch (Exception ex)
          {
              throw new ApplicationException(string.Format("BC caught '{0}'", ex.Message), ex);
          }
  </code></pre>
  
  <p>is executed in the button click, it produces the following output:</p>
  
  <p><em>BEGIN ThreadException handler</em></p>
  
  <p><em>error 'BC caught 'button click 1''</em></p>
  
  <ul>
  <li>error 'button click 1'*</li>
  </ul>
  
  <p><em>END ThreadException handler</em></p>
  
  <p>As you can see - entire exception chain is available.</p>
  
  <p>But ThreadException handler shows only original exception for very similar code in the RunWorkerCompleted event handler (code below was executed in the button click event):</p>
  
  <pre><code>        var bw = new BackgroundWorker();
          bw.DoWork += delegate(object o, DoWorkEventArgs args) 
          { 
              /* some calculations here */
          };
          bw.RunWorkerCompleted += delegate(object sender1, RunWorkerCompletedEventArgs eventArgs)
          { 
              try
              {
                  //in run worker completed exception occurs
                  Console.WriteLine("RWC exception test, throwing");
                  throw new ApplicationException("test exception 1");
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message);
                  Console.WriteLine("RWC got exception, throwing new");
                  throw new ApplicationException(string.Format("RWC caught '{0}'", ex.Message), ex);
              }
          };
  
          bw.RunWorkerAsync();
  </code></pre>
  
  <p>and output is below:</p>
  
  <p><em>RWC exception test, throwing</em></p>
  
  <p><em>test exception 1</em></p>
  
  <p><em>RWC got exception, throwing new</em></p>
  
  <p><em>BEGIN ThreadException handler</em></p>
  
  <p><em>error 'test exception 1'</em> -- only original exception here</p>
  
  <p><em>END ThreadException handler</em></p>
  
  <p>Seems that GetBaseException is executed only for RunWorkerCompleted event.</p>