What is the difference between OperationCanceledException and TaskCanceledException? If I am using .NET 4.5 and using the async/await keywords, which one should I be looking to catch?
What is the difference between OperationCanceledException and TaskCanceledException ? If I am using .NET
Share
OperationCanceledExceptionis simply the base class forTaskCanceledException– 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). SeeBlockingCollection.TryTakefor an example.I would catch the
OperationCanceledExceptionjust in case the task is cancelled due to an operation which itself just threwOperationCanceledException– you probably still want to treat that as “just cancellation”.