You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/keywords/when.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,6 +27,8 @@ catch (ExceptionType [e]) when (expr)
27
27
28
28
where *expr* is an expression that evaluates to a Boolean value. If it returns `true`, the exception handler executes; if `false`, it does not.
29
29
30
+
Exception filters with the `when` keyword provide several advantages over traditional exception handling approaches, including better debugging support and performance benefits. For a detailed explanation of how exception filters preserve the call stack and improve debugging, see [Exception filters vs. traditional exception handling](../statements/exception-handling-statements.md#exception-filters-vs-traditional-exception-handling).
31
+
30
32
The following example uses the `when` keyword to conditionally execute handlers for an <xref:System.Net.Http.HttpRequestException> depending on the text of the exception message.
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/statements/exception-handling-statements.md
+37Lines changed: 37 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -95,6 +95,43 @@ You can provide several `catch` clauses for the same exception type if they dist
95
95
96
96
If a `catch` clause has an exception filter, it can specify the exception type that is the same as or less derived than an exception type of a `catch` clause that appears after it. For example, if an exception filter is present, a `catch (Exception e)` clause doesn't need to be the last clause.
97
97
98
+
##### Exception filters vs. traditional exception handling
99
+
100
+
Exception filters provide significant advantages over traditional exception handling approaches. The key difference is **when** the exception handling logic is evaluated:
101
+
102
+
-**Exception filters (`when`)**: The filter expression is evaluated *before* the stack is unwound. This means the original call stack and all local variables remain intact during filter evaluation.
103
+
-**Traditional `catch` blocks**: The catch block executes *after* the stack is unwound, potentially losing valuable debugging information.
-**Better debugging experience**: Since the stack isn't unwound until a filter matches, debuggers can show the original point of failure with all local variables intact.
112
+
-**Performance benefits**: If no filter matches, the exception continues propagating without the overhead of stack unwinding and restoration.
113
+
-**Cleaner code**: Multiple filters can handle different conditions of the same exception type without requiring nested if-else statements.
114
+
-**Logging and diagnostics**: You can examine and log exception details before deciding whether to handle the exception:
Exception filters preserve the original `ex.StackTrace` property. If a `catch` clause can't process the exception and re-throws, the original stack information is lost. The `when` filter doesn't unwind the stack, so if a `when` filter is `false`, the original stack trace isn't changed.
132
+
133
+
The exception filter approach is valuable in applications where preserving debugging information is crucial for diagnosing issues.
134
+
98
135
#### Exceptions in async and iterator methods
99
136
100
137
If an exception occurs in an [async function](../keywords/async.md), it propagates to the caller of the function when you [await](../operators/await.md) the result of the function, as the following example shows:
0 commit comments