try {
await DoFooAsync();
} catch (Exception e) {
if(e is TaskCancelledException || e is UnauthorizedAccessException) {
// ...
}
throw;
}
The block catch
of the above excerpt checks the type of exception captured with a conditional block. As I will do the same treatment for TaskCancelledException
and UnauthorizedAcessException
, to avoid rewriting code with two catch
blocks with the same calls, I did as above.
However, there is another way to do it using statement when
. See:
try {
await DoFooAsync();
} catch (Exception e) when (e is TaskCancelledException || e is UnauthorizedAccessException) {
// ...
}
There is no practical difference. As far as I can tell, the two are the same.
What's the difference in running both? Which is better and why?