title: .NET 4.0 Tasks: Rethrow Exception to UI Thread in Task.ContinueWith
body: <p>I have a WPF application, where I make a long-running WCF call using the System.Threading.Tasks. I catch unhandled exceptions by adding a handler to Application.Current.DispatcherUnhandledException.</p>
  
  <p>I create a task, where the ContinueWith function runs on the UI thread using the following code:</p>
  
  <pre><code>var task = new Task&lt;T&gt;(func).ContinueWith(t =&gt;
  {
      if (t.IsFaulted)
      {
          throw t.Exception.GetBaseException();
      }
      else
      {
          // Show t.Result on UI
      }
  }, TaskScheduler.FromCurrentSynchronizationContext());
  </code></pre>
  
  <p>When an exception occurs in the task, I would like to rethrow the exception so that the DispatcherUnhandledException handler can handle it. But when I rethrow the exception, as shown above, it crashes my app and the DispatcherUnhandledException is not invoked.</p>
  
  <p>How do I rethrow the exception on the UI thread so that the DispatcherUnhandledException handler is invoked? </p>
  
  <p>When I was using the BackgroundWorker, rethrowing the exception did exactly this. Basically, I am looking to replace BackgroundWorker with Task, since Task has some really nice features I would like to take advantage of.</p>