Is catching TaskCanceledException and checking Task.Canceled a good idea?

前端 未结 1 427
清酒与你
清酒与你 2021-02-07 02:53

There are some people on my team who really love coding with async Task. And sometimes they like to use CancellationToken parameters.

What I\'

相关标签:
1条回答
  • 2021-02-07 03:19

    In the .Net framework itself when you pass a CancellationToken as a parameter you will get back a TaskCanceledException. I would not go against that and create my own design pattern because people who are familiar with .Net will be familiar with your code.

    My guideline is this: The one that cancels the token is the one that should handle the TaskCanceledException, so If you're using a CancellationToken inside your method for your own reasons, go ahead and use a try-catch block. But if you get the token as a parameter, let the exception be thrown.

    0 讨论(0)
提交回复
热议问题