-
Notifications
You must be signed in to change notification settings - Fork 213
Syntax catching multiple exceptions in a single on block #112
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
Comments
Being able to catch multiple exception in one block is really useful in my opinion to avoid duplication of code or noise on error management |
If you look at this more generally, a catch clause:
That's really similar to how pattern matching works in languages that have that. Most of those languages have some kind of We're considering adding tuples to Dart, and those almost always lead to some kind of pattern matching syntax in order to destructure the fields back out of the tuple. If we do that, one solution for the problem here would be to extend to catch clauses to allow arbitrary patterns. If we have some kind of "or" pattern, that would then let you have a single catch clause that matches objects of multiple types. |
Related link. |
One issue with allowing multiple |
I think that would feel comfortable for most authors since we hit it with other patterns too. |
That seems like a reasonable tradeoff. Any progress on this language feature? It seems that the discussion is already dead. |
No progress (it's not a particularly burning issue), but we are working on pattern matching again, it's possible that we'll incorporate some pattern stuff into |
Could something like: try {
doSomething();
} on ExceptionA, ExceptionB catch (e) {
fail(e);
} be syntactic sugar for: try {
doSomething();
} catch (e) {
if (e is ExceptionA || e is ExceptionB) {
fail(e);
} else {
rethrow;
}
} ? Edit: |
Perhaps you could leave it to the user to specify their preferred common parent for the variable when they name it in the |
In many cases I only care what type of exception I'm getting.
I don't care about the exception at-most I might want to do a
e.toString()
.Example:
This isn't critical at all, I was just surprised it didn't work already.
It's trivial to duplicate the block handling the exception, or write a function that accepts a list of types to ignore.
Please close if there is some complication to this that I don't see :)
The text was updated successfully, but these errors were encountered: