title: Exception handling on console application async code
body: <p>Hoping that someone will be able to point me in the right direction. I have a console application that uses async method calls and I'm having a bit of trouble with the try/catch error blocks. Basically, when a catch is triggered it executes the logging code within the catch but it still throws the error up to the top level and terminates the overall execution of the app.</p>
  <p>I've been looking through other queries about this and from what I've seen this type of behaviour can be caused by either an async void or an async Task with no await. Looking at my code I don't think I have either so I'm a bit stumped.</p>
  <p>The error being introduced is a missing connection string (in the context file, not shown), which should throw a handled exception in the PayloadRepo method. Stepping through the debugger this error is indeed caught and logged as expected, however it still seems to bubble back up to the Main method and cause the overall execution to break.</p>
  <p>Can anyone spot what may be happening?</p>
  <p>Thank you!</p>
  <pre><code>    static async Task Main(string[] args)
      {
          BuildLogConfiguration();
          BuildDependencyContainer();
  
          await RunProgram();
      }
  
      static async Task RunProgram()
      {
          systemLog.Info(&quot;Started full process at: &quot; + System.DateTime.Now);
  
          using (var scope = Container.BeginLifetimeScope())
          {
              var payloadService = scope.Resolve&lt;IPayloadService&gt;();
              await payloadService.ProcessPayloadData(scope);
          }
  
          systemLog.Info(&quot;Completing full process at: &quot; + System.DateTime.Now);
      }
  
      public class PayloadService : IPayloadService
      {
          private static readonly ILog _systemLog = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  
          public async Task ProcessPayloadData(ILifetimeScope scope)
          {
              await PayloadData_Process(scope);
          }
  
          private async Task PayloadData_Process(ILifetimeScope scope)
          {
              var repo = scope.Resolve&lt;IPayloadRepo&gt;();
  
              var payloadList = await repo.Payload_Get();
          }
      }
  
      public class PayloadRepo : IPayloadRepo
      {
          public async Task&lt;IEnumerable&lt;Payload&gt;&gt; Payload_Get()
          {
              using (var context = new ASGB_DataLayerContext())
              {
                  try
                  {
                      var payloadList = context.Payloads;
  
                      var result = await payloadList.ToListAsync();
  
                      return result;
                  }
                  catch (Exception ex) &lt;&lt;--- CATCHES THIS AND EXECUTES CODE WITHIN, BUT IT STILL THROWS AN ERROR ON RunProgram().Wait() AND TERMINATES PROGRAM
                  {
                      systemLog.Error(ex.Message);
                      systemLog.Error(ex.InnerException);
                      var entries = context.ChangeTracker.Entries().Where(e =&gt; e.State != EntityState.Unchanged);
                      foreach (var entry in entries)
                      {
                          foreach (var prop in entry.CurrentValues.Properties)
                          {
                              var val = prop.PropertyInfo.GetValue(entry.Entity);
                              systemLog.Error($&quot;{prop.ToString()} ~ ({val?.ToString().Length})({val})&quot;);
                          }
                      }
                      systemLog.Error(&quot;------------------------------------------------------------------------&quot;);
  
                      return null;
                  }
              }
          }
     }
  </code></pre>