title: Different exception handling between Task.Run and Task.Factory.StartNew
body: <p>I encountered an issue when I was using <code>Task.Factory.StartNew</code> and tried to capture an <code>exception</code> that is thrown. In my application I have a long running task that I want to encapsulate in a <code>Task.Factory.StartNew(.., TaskCreationOptions.LongRunning);</code>  </p>
  
  <p>However, the exception isn't caught when I'm using <code>Task.Factory.StartNew</code>. It is however working as I expect when I use <code>Task.Run</code>, which I thought was just a wrapper on <code>Task.Factory.StartNew</code> (according to for instance <a href="http://msdn.microsoft.com/en-us/library/hh873173.aspx">this MSDN article</a>).</p>
  
  <p>A working example is provided here, the difference being that the exception is written to console when using <code>Task.Run</code>, but not when using <code>Factory.StartNew</code>.</p>
  
  <p><strong>My question would be:</strong><br>
  if I have a <code>LongRunning</code> task that has the possibility to throw exceptions, how should I handle them in the calling code?</p>
  
  <pre><code>private static void Main(string[] args)
  {
      Task&lt;bool&gt; t = RunLongTask();
      t.Wait();
      Console.WriteLine(t.Result);
      Console.ReadKey();
  }
  
  private async static Task&lt;bool&gt; RunLongTask()
  {
      try
      {
          await RunTaskAsync();
      }
      catch (Exception e)
      {
          Console.WriteLine(e);
          return false;
      }
      Console.WriteLine("success");
      return true;
  }
  
  private static Task RunTaskAsync()
  {
      //return Task.Run(async () =&gt;
      //    {
      //        throw new Exception("my exception");
      //    });
      return Task.Factory.StartNew(
          async () =&gt;
      {
          throw new Exception("my exception");
      });
  
  }
  </code></pre>