title: Catching an exception in an async method
body: <p>I am trying to get more familiar with async/await programming and exception handling.</p>
  
  <pre><code>static void Main(string[] args)
  {
     TestAsyncException();
  }
  
  private static async void TestAsyncException()
  {
      try
      {
          var result = await Task.Factory.StartNew(() =&gt; DoSomething());
          //do something with the result
      }
      catch (Exception e)
      {
          Console.WriteLine(e.Message);
      }
  }
  
  private static int DoSomething()
  {
      throw new Exception("Exception was thrown!");
  }
  </code></pre>
  
  <p>I am expecting the <code>Exception</code> to be handled gracefully but instead the code execution stops and I get </p>
  
  <blockquote>
    <p>An exception of type 'System.Exception' occurred in .. but was not
    handled in user code. </p>
  </blockquote>
  
  <p>But then when I continue executing the code the <code>Exception</code> actually gets caught (and the message is displayed to the <code>Console</code>).</p>
  
  <p>How am I supposed to catch the <code>Exception</code> without breaking the execution of my code?</p>