Why can't I catch an exception from async code?

前端 未结 5 1887
南旧
南旧 2020-11-28 11:20

Everywhere I read it says the following code should work, but it doesn\'t.

public async Task DoSomething(int x)
{
   try
   {
      // Asynchronous implemen         


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 11:38

    Instead of using await, access the Task.Result property and put a try and catch around that access. You can also follow the example here and try that style.

    Keep in mind that all exceptions thrown inside the context of a task thread are wrapped in an AggregateException.

提交回复
热议问题