13

Is there a difference when catching exceptions not of a type between :

try
{
    ...
}
catch (TaskCanceledException)
{
    throw;
}
catch (Exception exception)
{
    ...
}

and :

try
{
    ...
}
catch (Exception exception) when (!(exception is TaskCanceledException))
{
    ...
}
Troopers
  • 5,127
  • 1
  • 37
  • 64
  • 1
    First one snippet will catch all exception with `TaskCanceledException` and in the second snippet will catch all exception except `TaskCanceledException` – er-sho May 02 '19 at 07:45
  • 2
    Them can have different order of execution of `finally` clauses placed inside `try` block: https://tio.run/##7VI9T8MwEN3zK45OjgRRm5GIKWKDiUjMrnMlFsaubIdQVf7twalCPsBtQTByi3Pv5Z7vfI@ZK6Y0tm1tuHyCh52x@JJF0ywpKo209EBSUPNssmhbrwVnwAQ1BnLYR@CjB42l1h@vipdwT7kkcc93UaCxKxJnMyD9ANwxnb5sIsSlBQ43sByl1koJ2Mz@6iJX0iiByaPmFu@4RLI4yF3DfukWl8An3XSh0dZagtU1joQbvqzefbrAI7N8zoZrhjH8CKssSNlKqwYkNtA9ek4lQ4Hl7RvDreVKkvhrmYMNl1SIU7elgbLoeMaoZRWQYAvxubG7CbLz4oMg4HekRw3XKzQVSuI371ffsyedlP6tk9J/J/3ASaFlHxYI5IIMEHATbjb@vTNc274D – user4003407 May 02 '19 at 08:02
  • @PetSerAI interesting example, can you provide more reference to this behaviour, so in catch with filter the finally is executed at the last of all exception blocks? while in catch without filter, this is nested? – peeyush singh May 02 '19 at 08:15
  • Both these methods are implemented differently in CLR (check the ILDASM) in the second one, first the when part is executed and on the basis of that the catch block will be entered. Have a look at: https://weblogs.asp.net/dixin/c-6-0-exception-filter-and-when-keyword – peeyush singh May 02 '19 at 08:35
  • @peeyushsingh System first search for a `catch` clause, which can handle exception. In doing so, system execute exception filters to check if `catch` is applicable. Only then nested `finally` clauses will be executed. It all in [specification](https://github.com/dotnet/csharplang/blob/master/spec/exceptions.md#how-exceptions-are-handled), although it is a bit outdated. Before exception filters became part of C#. Also, you can read [this](https://learn.microsoft.com/dotnet/framework/misc/securing-exception-handling). – user4003407 May 02 '19 at 08:39

3 Answers3

-1

Refer to this link Stackoverflow

Catch blocks already allow you to filter on the type of the exception:

catch (SomeSpecificExceptionType e) {...}

The when clause allows you to extend this filter to generic expressions.

Thus, you use the when clause for cases where the type of the exception is not distinct enough to determine whether the exception should be handled here or not.

Daniel Attard
  • 102
  • 1
  • 8
-1

Yes, there is.

In the second block of code you are filtering on general exception type. It is not meant to be filtered, what happens if "TaskCanceledException" is thrown? you are not handling it and it will escalate to enclosing code. You are not really meant to filter anything on "Exception" type since it is the parent for all other type of exception and it is the last point of handling exception. A better option would be to create a custom exception and put them in separate block of catch and filter on them if needed.

The first option is more correct compared to the second one. Though you should not be throwing any exception and stay away from them, unless it is a complete deal breaker. On top of that what is the point of putting a catch block with Exception type bellow TaskCanceledException catch block which throws exception? You are basically telling that "I Would like to handle all exception" when using catch with Exception type, but at the same time you through one exception in exceptional case. Either throw the original exception or handle them.

Hope this makes sense.

vsarunov
  • 1,433
  • 14
  • 26
  • 3
    This answer doesn't make much sense and doesn't answer the question. Not sure why the OP marked it as accepted. – Mo B. Jun 14 '22 at 08:59
  • indeed, it's very opinionated, if you're filtering exceptions it's not unlikely that you actually want some of them to be passed to the calling code and only handle what's in direct control of that method – mikus Jul 12 '23 at 09:54
-1

The Dixin's Blog explained the difference with code example and VS screenshots.

Exception filter does not unwind the stack, and catch block does unwind. When executing catch block, this catch block’s method becomes the top frame of the stack. As a result, all the methods called by current method are removed from the stack. In contrast, exception filter can be helpful for runtime debugging. For example, if above Catch method is executed:

Tony
  • 1,827
  • 1
  • 22
  • 23