async/await exception handling pattern

前端 未结 1 837
无人共我
无人共我 2020-12-15 16:23

I have the following reoccurring try/catch pattern in my code. Using a try/catch block to handle any exceptions thrown when calling a method in orionProxy.

a         


        
相关标签:
1条回答
  • 2020-12-15 16:46

    I don't see why it wouldn't work, assuming GetContacts is an async method:

    public async Task<T> CheckForExceptionAsync<T>(Task<T> source)
    {
      try
      {
        return await source;
      }
      catch (Exception ex)
      {
        HandleException(ex);
        return default(T);
      }
    }
    

    On a side note, you should avoid async void (as I describe in my MSDN article) and end your async method names with the Async suffix.

    0 讨论(0)
提交回复
热议问题