Async exception not being caught or being swallowed

前端 未结 3 1122
自闭症患者
自闭症患者 2021-01-23 09:26

Update from the future: TL;DR to catch expressions in async methods you have to await, Task.WaitAll, or .Result.

3条回答
  •  伪装坚强ぢ
    2021-01-23 09:53

    You can handle unobserved Task exceptions as follows:

    TaskScheduler.UnobservedTaskException += (object sender, UnobservedTaskExceptionEventArgs eventArgs) =>
    {
          eventArgs.SetObserved();
          ((AggregateException)eventArgs.Exception).Handle(ex =>
          {
              //TODO: inspect type and handle exception
              return true;
          });
    };
    

提交回复
热议问题