Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
Proper way to correlate an OperationCanceledException to a CancellationToken

I'm using an async api which supports cancellation and I'm passing to that api a CancellationToken instance. As usual, if the cancellation is requested on the passed token, the api that I'm invoking will throws an OperationCanceledException (this is the standard cooperative cancellation pattern of the .NET framework).

I want to be able to catch the OperationCanceledException if and only if it is the exception raised due to the cancellation of the provided cancellation token.

The following code illustrates what I'm trying to achieve:

try 
{
    await _service.DoSomethingAsync(cancellationToken: token);
}
catch (OperationCanceledException ex) when ( /* here I want a condition signifying that the OperationCanceledException is caused by the cancellation of the token object */)
{
    // avoid logging the exception: this is raised by design (cooperative cancellation)
    throw;
}
catch (Exception ex) 
{
    _logger.LogError(ex, "An error occurred: {0}", ex.Message);
    throw;
}

For the exception filter of the code above I have basically two ideas:

  • checking the IsCancellationRequested property on the token object: when (token.IsCancellationRequested)
  • checking the CancellationToken property on the ex object: when (ex.CancellationToken == token)

What is the right way to do the check I want to perform ? Are the two ways showed above equivalent ? Is there a best practice ?

IMPORTANT NOTE: I know that the code showed above can be written in a more efficient way, because catching exceptions is an expensive operation. The best thing to do is probably removing the first catch block at all, and only catching an Exception if and only if it is not related with the cancellation of the token object. I'm aware of that, but that's not the point of my question. I have written the code in the question that way only for clarity, because the whole point of my question is how to properly correlate an OperationCanceledException with the CancellationTokenwhich has caused the exception itself.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I want to be able to catch the OperationCanceledException if and only if it is the exception raised due to the cancellation of the provided cancellation token.

You can't do this exactly, but you can "catch the canceled exception only if my provided token has been cancelled", which is usually good enough.

when (token.IsCancellationRequested) is what you want.

Don't check the ex.CancellationToken, because the method you're calling may be observing a linked cancellation token, which would be different than the one you provided.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!