-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Proposal: Pattern-based exception-handling #6789
Comments
@stepanbenes Yeah I know. |
As you have currently specified them, OR patterns require pattern variables to be of the same type on both sides of the disjunction, so using them to catch multiple exception types is not easy. |
@gafter I don't know if OR patterns could use the "most common type" rule? (that would be really nice, by the way) However if they could, still, you have to repeat the pattern variable for each exception like |
@alrz if we used the existing dominant type rules, one of the two types would be required to be a subtype of the other. I do not think you would like that. I think you meant disjunctive, not conjunctive types, as it is only one or the other at any given time. When I did it for Java I used |
When I specified and implemented this for Java, you would write try {
...
} catch (NullReferenceException | InvalidCastException ex) {
...
} The type specified in the catch clause can be thought of as a disjunction type. The compiler would emit code to catch precisely those two exception types, and the type of the variable That works in Java because
C#'s "common type" algorithm (as currently specified) never produces a type that is not one of the input types. |
Issue moved to dotnet/csharplang #335 via ZenHub |
Since
catch
already has an optional "exception filter", I think it would be reasonable to use patterns also incatch
clauses.All patterns must be pattern compatible with the type
Exception
.Instead of
If you declare exceptions using record syntax, then you will be able to use recursive-pattern:
With OR patterns (#6235) one should be able to check for multiple exceptions without exception filters.
The text was updated successfully, but these errors were encountered: