C# 6.0 introduces a new feature exception filter. As per MSDN “Exception Filters are clauses that determine when a given catch clause should be applied. If the expression used for an exception filter evaluates to true, the catch clause performs its normal processing on an exception.If the expression evaluates to false, then the catch clause is skipped.”
1
2
| // Syntax catch (Exception ex) when (FilterIsTrue). |
C# 6.0 exception filter and when keyword is not a syntactic sugar. It is a CLR feature. Unlike catch block, exception filter does not unwind the call stack, which is helpful at runtime.
So what is Stack unwinding?
Stack unwinding
When you enter a catch block, the stack is unwound: this means that the stack frames for the method calls “deeper” than the current method are dropped. This implies that all information about current execution state in those stack frames is lost, making it harder to identify the root cause of the exception.
Exception filter does not unwind the call stack.
See the screen shot.
See the screen shot.