2
try {
await DoFooAsync();
} catch (Exception e) {
if(e is TaskCancelledException || e is UnauthorizedAccessException) {
// ...
}
throw;
}
The block catch
from the above section checks the type of exception captured with a conditional block. As I will do the same treatment for TaskCancelledException
and UnauthorizedAcessException
, to avoid code rewriting with two blocks catch
with the same calls, I did as above.
However, there is another way to do it using the statement when
. Behold:
try {
await DoFooAsync();
} catch (Exception e) when (e is TaskCancelledException || e is UnauthorizedAccessException) {
// ...
}
There is no practical difference. As far as I could see the two result the same.
What is the difference in the execution of the two? Which is preferable and why?
Great reference :P
– vinibrsl
You can put an example in the codeground (or anywhere else) that proves that the stack trace is changed?
– Bruno Costa
@Brunocosta is a question for the Debugger, then there’s no way to demonstrate this other than in VS or something like that. Read this: https://roslyn.codeplex.com/discussions/541301
– Maniero
I understood when I read it I thought I was saying that I really changed the value. After all it is a question only of the Bugger.
– Bruno Costa