title: Exceptions not being caught in Async/Await Task operations
body: <p>I'm writing a ASP.NET MVC site in .NET Core. I'm trying to encapsulate some common exception handling. In a Base class I have this method.</p>
  
  <pre><code>public abstract class BaseController&lt;TController&gt; : Controller where TController : Controller
  {
      protected IActionResult ExceptionHandledOperation(Func&lt;IActionResult&gt; operation, Func&lt;IActionResult&gt; handleException)
      {
          try
          {
              return operation.Invoke();
          }
          catch (Exception exception)
          {
              Logger.LogError($"Operation {Request.Path} Exception", exception);
  
              return handleException.Invoke();
          }
      }
  }
  </code></pre>
  
  <p>From a controller that inherits from this base class, I utilize this method like so:</p>
  
  <pre><code>[Route("api/[controller]")]
  public class MyController : BaseController&lt;MyController&gt;
  {
      [HttpGet]
      public IActionResult Get()
      {
          return ExceptionHandledOperation(() =&gt; Ok(_someService.GetAsync().Result),
                                           NotFound);
      }
  }
  </code></pre>
  
  <p>Assume the _someService.GetAsync() method is this:</p>
  
  <pre><code>public class SomeService
  {
      public async Task&lt;PreconfigurationData&gt; GetAsync()
      {
          // http request removed for brevity
  
          if (!response.IsSuccessStatusCode)
              throw new Exception("SomeService Exception");
      }
  }
  </code></pre>
  
  <p>This worked fine and would catch my exception in the base class method and return the NotFound result.</p>
  
  <p>However, I wanted to avoid calling .Result from the SomeService.GetAsync method. Everywhere I read it says not to do that as it can deadlock.</p>
  
  <p>So I modified my base controller to this:</p>
  
  <pre><code>public abstract class BaseController&lt;TController&gt; : Controller where TController : Controller
  {
      protected async Task&lt;IActionResult&gt; ExceptionHandledOperationAsync(Func&lt;IActionResult&gt; operation, Func&lt;IActionResult&gt; handleException)
      {
          try
          {
              return await Task.Run(() =&gt; operation.Invoke());
          }
          catch (Exception exception)
          {
              Logger.LogError($"Operation {Request.Path} Exception", exception);
  
              return await Task.Run(() =&gt; handleException.Invoke());
          }
      }
  }
  </code></pre>
  
  <p>And MyController like this:</p>
  
  <pre><code>[Route("api/[controller]")]
  public class MyController : BaseController&lt;MyController&gt;
  {
      [HttpGet]
      public async Task&lt;IActionResult&gt; Get()
      {
          return await ExceptionHandledOperationAsync(() =&gt; Ok(_someService.GetAsync()),
                                                      NotFound);
      }
  }
  </code></pre>
  
  <p>However, my exception thrown from my SomeService.GetAsync method is never caught and I never get the NotFound response I intend to send when handling the exception.</p>
  
  <p>Everywhere I read it says you just need to await the task in the try and then any exceptions will be caught, but mine never does.</p>
  
  <hr>
  
  <p><strong>Solved</strong></p>
  
  <p>I was finally able to get this resolved. Thanks to Tseng for the help.</p>