title: How to handle Task.Factory.StartNew exception?
body: <p>I have some strange problem here. Exception thrown by Task always not handled independently how can I try to handle it.</p>
  
  <p>I try this:</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/dd997415%28v=vs.110%29.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/dd997415%28v=vs.110%29.aspx</a></p>
  
  <pre><code>private class MyCustomException : Exception
  {
       public MyCustomException(String message) : base(message)
       {
       }
  }
  
  public static void Main()
  {
     var task1 = Task.Factory.StartNew(() =&gt;
     {
  
        throw new MyCustomException("I'm bad, but not too bad!");
     });
  
     try
     {
        task1.Wait();
     }
     catch (AggregateException ae)
     {
         // Assume we know what's going on with this particular exception. 
         // Rethrow anything else. AggregateException.Handle provides 
         // another way to express this. See later example. 
         foreach (var e in ae.InnerExceptions)
         {
             if (e is MyCustomException)
             {
                 Console.WriteLine(e.Message);
             }
             else
             {
                 throw;
             }
         }
  
      }
      Console.Read();
  }
  </code></pre>
  
  <p>this:</p>
  
  <p><a href="http://dotnetcodr.com/2014/02/11/exception-handling-in-the-net-task-parallel-library-with-c-the-basics/" rel="noreferrer">http://dotnetcodr.com/2014/02/11/exception-handling-in-the-net-task-parallel-library-with-c-the-basics/</a></p>
  
  <p>this:</p>
  
  <p><a href="http://blogs.msdn.com/b/pfxteam/archive/2010/08/06/10046819.aspx" rel="noreferrer">http://blogs.msdn.com/b/pfxteam/archive/2010/08/06/10046819.aspx</a></p>
  
  <p>this:</p>
  
  <pre><code>var task = Task.Factory.StartNew(() =&gt; this.InitializeViewModel(myViewModel));
  task.ContinueWith(o =&gt; MyErrorHandler(task.Exception), TaskContinuationOptions.OnlyOnFaulted);
  </code></pre>
  
  <p>and check a lot of other similar questions on StackOverflow. But It is always the same - exception is not handled. It is not handled on these primitive code snippets! I think it is some magic here...
  I work on .Net Framework 4.0</p>
  
  <p>Meanwhile single way to handle exception that works for me is:</p>
  
  <pre><code>        Task.Factory.StartNew(() =&gt;
          {
              try
              {
                  //do something that thrown exception
              }
              catch (Exception)
              {
  
              }
          });
  </code></pre>