- 
                Notifications
    You must be signed in to change notification settings 
- Fork 6.1k
Add note about predicate expressions #5634
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
Conversation
I feel that quite a lot of uses of the `throw;` statement can be written in a more clean fashion by using a predicate expression (`catch (...) when(condition)`), so I've added a note in the try-catch documentation about it.
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 great addition @sylveon We'd like to publicize the exception filter feature and encourage its use where it creates simpler code.
Over time, I want to update all the samples to show the preferred behavior.
I did have one change request.  Can you just push that change to the same branch? Then, I'll 
Thank you for contributing to docs. 🎆
|  | ||
|  | ||
| > [!NOTE] | ||
| > It is also possible to use a predicate expression to get a similar result in a often cleaner fashion. The following example has a similar behavior for callers than the precedent example, the function throws the `InvalidCastException` back to the caller when `e.Data` is null. | 
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.
Instead of "predicate expression", I'd prefer the term "exception filter".
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.
Some additional suggested changes, @sylveon:
- a often -->an often
- than the precedent example, --> as the previous example.
- the function --> The function
- null --> `null`
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 was following the name used a bit before in the same document.
Using catch arguments is one way to filter for the exceptions you
want to handle. You can also use a predicate expression that further
examines the exception to decide whether to handle it. If the
predicate expression returns false, then the search for a handler continues.
Should I change that too while I'm at it @BillWagner?
@rpetrusha will do.
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.
thanks @sylveon Yes, let's change it in both locations
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.
Done and done.
| Thanks for making this changes @sylveon I'll  Thanks again for contributing to docs! 🎆 | 
Summary
I feel that quite a lot of uses of the
throw;statement can be written in a more clean fashion by using a predicate expression (catch (...) when(condition)), so I've added a note in the try-catch documentation about it.