-
Notifications
You must be signed in to change notification settings - Fork 213
Make each "parameter" of a catch clause optional #3815
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
CC @kallentu @dart-lang/analyzer-team |
For what it's worth, you can do this if you don't care about the error: try {
// ...
} on Object {
// ...
} |
Yeah |
Hmm, |
Also quite common are |
Sort of. As far as the empty body goes, we have a lint rule, |
Good point. It's worth noting that it just takes a comment to silence the lint: try {
...
} catch(e) {
// ignored, really.
} With wildcards, I'd expect the comment to be unnecessary and would hope that try {
...
} catch(_) { } wouldn't trigger it... But that's up for discussion! (dart-lang/sdk#57132) |
FWIW, even though in principle I know that I would be tempted to take things one step further and allow not just |
It's not a function, so that one doesn't bother me. It's consistent with The underlying idea was that if a catch clause doesn't need a stack trace, and exception handling generally shouldn't, then the runtime system doesn't have to create a stack trace object at all. I'm not sure allowing What I would really want is to allow patterns in catch clauses: } catch (StateError e, s) when e.toString().contains("banana") {
...
} which would be able to inspect the thrown object more than just by its type, before deciding whether to catch or not, instead of catching and maybe rethrowing. Each such catch clause is checked against Another thing I'd like to do is to remove that stack trace from the |
I would l love that. Also I think the I also regularly use the |
Today catch "parameters" are kind of special creatures. You can specify one,
catch (e) <body>
, and the catch body will be "called" with one argument. Or you can specify two,catch (e, st) <body>
, and the catch body will be "called" with two arguments. The Dart backend is happy to oblige. But you cannot specify zero parameters. 🤷It seems to be an inconsistent syntax (I might be missing some justification). Firstly, it is inconsistent with how functions work. I think the simplest example is callbacks: The
Iterable.forEach
method takes avoid Function(E)
. If you don't want to use the parameter, you don't get to pass avoid Function()
. You might imagine that you could pass() => print('yay')
, and that Dart would just call your callback with no arguments, no problem. But no, you just get a compile-time error. You might similarly think that an update toIterable.forEach
could start to accept functions of the shapevoid Function(E, {int index})
, like(_, index) => print('yay $index')
, and that such an update would be non-breaking because Dart could, similar toFuture.catchError
orcatch
, either pass one argument or two. But no, you just get a compile-time error.OK, no problem, that's how functions work in Dart. But
catch
(and some others likeFuture.catchError
) allow you some wiggle room, presumably for some purpose. Maybe it's a big burden to make people writecatch (e, st) <body>
every time they oh-so-frequently write catch clauses, so we let them writecatch (e) <body>
. (Maybe we save some cycles by avoiding instantiating a StackTrace 🤷 ) OK, no problem. So if I don't want to accept a StackTrace, I don't have to accept a StackTrace. What if I don't want to accept the error?In current Dart, I'm out of luck. I'm stuck writing such lengthy or verbose code like
catch (e) <body>
orcatch (_) <body>
. Por que? It seems quite inconsistent to go to lengths to step away from function calling conventions and allow optional "parameters," but only for this one "parameter," and not that one "parameter."I think this could be tied to the Wildcards feature. It would be non-breaking, so safe to sneak in. And it's related because of what analyzer can start reporting with this feature. With wildcards + no change to
catch
, analyzer could report warnings that seems to only point out the language inconsistency. When a user writescatch (e, st) {}
, they could get two messages that are head-scratching seen togother:The text was updated successfully, but these errors were encountered: