title: How to catch an Exception throw by Azure Table in an async Azure Function HTTP Triggered function
body: <p>I have an Azure Function HTTP triggered function which writes to Azure Table that may end in duplicated entries. I noticed that even if I try/catch'd the whole function, there will still be an Exception "leaked" to the function runner thus returning HTTP 500. Is there any way to catch this kind of exception?</p>
  
  <p>Here's a minified version of the code:</p>
  
  <pre><code>using System.Threading.Tasks;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.Azure.WebJobs;
  using Microsoft.Azure.WebJobs.Extensions.Http;
  using Microsoft.AspNetCore.Http;
  using Microsoft.Extensions.Logging;
  using Microsoft.WindowsAzure.Storage;
  
  namespace FunctionTest
  {
      public class Entry
      {
          public string PartitionKey { get; set; }
          public string RowKey { get; set; }
      }
      public static class Debug
      {
          [FunctionName("Debug")]
          public static async Task&lt;IActionResult&gt; Run(
              [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]
              HttpRequest req,
              [Table("Debug")]
              IAsyncCollector&lt;Entry&gt; tableBinding,
              ILogger log)
          {
              try
              {
                  await tableBinding.AddAsync(new Entry()
                  {
                      PartitionKey = "1111",
                      RowKey = "1111",
                  });
                  await tableBinding.FlushAsync();
              }
              catch (StorageException)
              {
                  // we expect an Exception "The specified entity already exists"
                  return new OkObjectResult("This passes test");
              }
  
              return new OkObjectResult("This passes test too");
          }
      }
  }
  </code></pre>
  
  <p>The code is written under Azure Function runtime 2.0 (the .NET Core one).</p>
  
  <p>Trigger <code>/api/debug</code> twice or more and you will see:</p>
  
  <ul>
  <li>HTTP 500</li>
  <li>The <code>catch{}</code> code is entered, and still returns an HTTP 500(!)</li>
  <li>In Application Insights, two table dependency call per request (shouldn't happen, the documentation says table do not have auto retry)</li>
  </ul>