Exception in async methods is not caught

前端 未结 2 1853
面向向阳花
面向向阳花 2021-01-27 11:48

The following code does not catch my OperationCancelException which is thrown by calling ct.ThrowIfCancellationRequested.

public partial class Title         


        
2条回答
  •  太阳男子
    2021-01-27 12:37

    Exeption is thrown in the task on another thread.

    public async Task GetCancelExceptionAsync(CancellationToken ct)
            {
                try
                {
                    await Task.Delay(1000);
                    ct.ThrowIfCancellationRequested();
                }
                catch (Exception e)
                {
                    // your Cancleation expeption
                }
            }
    

提交回复
热议问题