-
Notifications
You must be signed in to change notification settings - Fork 6k
Add explanation of why the when
contextual keyword is better than if/else in catch blocks
#47887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: BillWagner <[email protected]>
when
contextual keywordwhen
contextual keyword is better than if/else in catch blocks
...p/language-reference/statements/snippets/exception-handling-statements/WhenFilterExamples.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good start.
docs/csharp/language-reference/statements/exception-handling-statements.md
Outdated
Show resolved
Hide resolved
docs/csharp/language-reference/statements/exception-handling-statements.md
Outdated
Show resolved
Hide resolved
...p/language-reference/statements/snippets/exception-handling-statements/WhenFilterExamples.cs
Show resolved
Hide resolved
Co-authored-by: BillWagner <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code comments should end in punctuation.
- **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. | ||
- **Traditional `catch` blocks**: The stack is unwound *before* entering the catch block, potentially losing valuable debugging information. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Before" is italicized in both bullet points to add emphasis, which doesn't make much sense since they're trying to point out differences.
|
||
:::code language="csharp" source="snippets/exception-handling-statements/WhenFilterExamples.cs" id="ExceptionFilterVsIfElse"::: | ||
|
||
**Advantages of exception filters:** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know Copilot likes to add bolding using **
, but it actually goes against style guidelines.
|
||
**Advantages of exception filters:** | ||
|
||
1. **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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be a numbered list since it's not a sequential procedure.
- Handle exceptions based on specific conditions or properties | ||
- Preserve the original call stack for debugging | ||
- Log or examine exceptions before deciding whether to handle them | ||
- Handle the same exception type differently based on context |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add periods.
This PR addresses the lack of explanation for why C#'s
when
contextual keyword provides advantages over using if/else statements inside catch blocks. The key technical difference is that exception filters don't unwind the call stack, while traditional catch blocks do.Changes Made
Enhanced exception-handling-statements.md
Added a comprehensive "Exception filters vs. traditional exception handling" section that explains:
when
) evaluate before stack unwinding, preserving the original call stack and local variablesUpdated when.md
Added context explaining the advantages of exception filters with a cross-reference to the detailed explanation in the exception handling statements documentation.
New Code Examples
Created comprehensive working examples in
WhenFilterExamples.cs
demonstrating:Technical Benefits Explained
The documentation now clearly explains why this approach is superior:
The exception filter approach is particularly valuable in production applications where preserving debugging information is crucial for diagnosing issues.
Fixes #40661.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
Internal previews