OperationCanceledException 与 TaskCanceledException 的区别? ?

OperationCanceledExceptionTaskCanceledException有什么不同?如果我在吸毒。NET 4.5和使用 async/await关键字,我应该寻找捕捉哪一个?

16019 次浏览

OperationCanceledException is simply the base class for TaskCanceledException - so if you catch the former, you'll still catch the latter.

Some operations on concurrent collections throw just OperationCanceledException, as there aren't any actual tasks involved (at least as far as the public API is concerned). See BlockingCollection.TryTake for an example.

I would catch the OperationCanceledException just in case the task is cancelled due to an operation which itself just threw OperationCanceledException - you probably still want to treat that as "just cancellation".