title: Catching Exceptions in async methods when not called with await
body: <h2>Goal:</h2>
  
  <p>I am confused by the behavior I am seeing with exceptions in my .Net Core library.  The goal of this question is to understand <strong>why</strong> it is doing what I am seeing.</p>
  
  <h2>Executive Summary</h2>
  
  <p>I thought that when an <code>async</code> method is called, the code in it is executed synchronously until it hits the first await. If that is the case, then, <strong>if an exception is thrown during that "synchronous code", why is it not propagated up to the calling method? (As a normal synchronous method would do.)</strong></p>
  
  <h2>Example Code:</h2>
  
  <p>Given the following code in a .Net Core Console Application:</p>
  
  <pre><code>static void Main(string[] args)
  {
      Console.WriteLine("Hello World!");
  
      try
      {
          NonAwaitedMethod();
      }
      catch (Exception e)
      {
          Console.WriteLine("Exception Caught");
      }
  
      Console.ReadKey();
  }
  
  public static async Task NonAwaitedMethod()
  {
      Task startupDone = new Task(() =&gt; { });
      var runTask = DoStuff(() =&gt;
      {
          startupDone.Start();
      });
      var didStartup = startupDone.Wait(1000);
      if (!didStartup)
      {
          throw new ApplicationException("Fail One");
      }
  
      await runTask;
  }
  
  public static async Task DoStuff(Action action)
  {
      // Simulate starting up blocking
      var blocking = 100000;
      await Task.Delay(500 + blocking);
      action();
      // Do the rest of the stuff...
      await Task.Delay(3000);
  }
  </code></pre>
  
  <p>}</p>
  
  <h2>Scenarios:</h2>
  
  <ol>
  <li><p>When run as is, this code will throw an exception, but, unless you have a break point on it, you will not know it.  The Visual Studio Debugger nor the Console will give any indication that there was an issue (aside from a one line note in the Output screen).</p></li>
  <li><p>Swap the return type of <code>NonAwaitedMethod</code> from <code>Task</code> to <code>void</code>.  This will cause the Visual Studio Debugger to now break on the exception.  It will also be printed out in the console.  But notably, the exception is <strong>NOT</strong> caught in the <code>catch</code> statement found in <code>Main</code>.</p></li>
  <li><p>Leave the return type of <code>NonAwaitedMethod</code> as <code>void</code>, but take off the <code>async</code>.  Also change the last line from <code>await runTask;</code> to <code>runTask.Wait();</code> (This essentially removes any async stuff.)  When run, the exception is caught in the <code>catch</code> statement in the <code>Main</code> method.</p></li>
  </ol>
  
  <p>So, to summarize:</p>
  
  <pre><code>| Scenario   | Caught By Debugger | Caught by Catch |  
  |------------|--------------------|-----------------|  
  | async Task | No                 | No              |  
  | async void | Yes                | No              |  
  | void       | N/A                | Yes             |  
  </code></pre>
  
  <h2>Questions:</h2>
  
  <p>I thought that because the <strong>exception was thrown before an <code>await</code> was done</strong>, that <strong>it would execute synchronously</strong> up to, and through the throwing of the exception.</p>
  
  <p>Hence my question of: <strong>Why does neither scenario 1 or 2 get caught by the <code>catch</code> statement?</strong></p>
  
  <p>Also, why does swapping from <code>Task</code> to <code>void</code> return type cause the exception to get caught by the Debugger?  (Even though I am not using that return type.)</p>