title: Catching Error when using Task.Factory
body: <p>i am using the following</p>
  
  <pre><code>Task.Factory.StartNew(() =&gt; DoPrintConfigPage(serial));
  </code></pre>
  
  <p>then the function i am calling looks like this</p>
  
  <pre><code>private void DoPrintConfigPage(string serial) 
  { 
      //do printing work 
  }
  </code></pre>
  
  <p>My problem is an exception is being thrown inside the thread and not being handled.</p>
  
  <p>I have tried wrapping it in a try catch </p>
  
  <pre><code>try
  {
      Task.Factory.StartNew(() =&gt; DoPrintConfigPage(serial));
  }
  catch (Exception ex) { }
  </code></pre>
  
  <p>but it still is not catching the error and thus crashing the application.</p>
  
  <p>How can I catch exceptions in the main thread so I can handle them?</p>
  
  <h2>Update</h2>
  
  <p>I have made the changes recommended below and still it is saying the exception is unhandled</p>
  
  <pre><code>var task =  Task.Factory.StartNew(() =&gt; DoPrintConfigPage(serial))
                                 .ContinueWith(tsk =&gt;
                                 {
                                    MessageBox.Show("something broke");
                                 },TaskContinuationOptions.OnlyOnFaulted);
  </code></pre>
  
  <p>then in my <code>DoConfigPage</code> I added another try catch. </p>
  
  <p>In this catch is now where it is crashing and saying the exception being thrown was unhandled, what am I missing?</p>
  
  <pre><code>private void DoPrintConfigPage(string serial)
  {
      try
      {
          //call the print function
      }
      catch (Exception ex)
      {
          throw ex;   //it is crashing here and saying it is unhandled
      }
  }
  </code></pre>
  
  <p>I also tried what Eric J. suggested with the same results</p>
  
  <pre><code>var task = Task.Factory.StartNew(() =&gt; DoPrintConfigPage(serial));
  
  try
  {
      task.Wait();                  
  }
  catch (AggregateException ex) { MessageBox.Show("something broke"); }
  </code></pre>