-
Notifications
You must be signed in to change notification settings - Fork 207
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
Inline catching #4205
Comments
Something I don't really like is that catch will by default catching any kind of thrown object which includes So because of that, the suggested syntax would most often end up looking like: final int example = int.parse("123") catch on Exception 0; I feel the readability ends up being less clear when this is needed. And because of that, I think most people would end up just skipping the I therefore like the suggested syntax. But my problems with it are not really directly related to the syntax itself but more the overall design of handling thrown objects in Dart and the consequences of making it too easy. (and yes, I know we already have the problem with the existing syntax since I every day see people doing |
That's very true. In principle, this suggestion is about letting users quickly and easily recover from small, anticipated errors: you probably don't want to be recovering from an OOM error when all you wanted to do was parse an integer. That said, I actually quite like the And while this is already an issue, there's the complication of not knowing what to specify as the catch-type since Dart doesn't require nor even allow functions to be defined with what exceptions it may throw (like Java), so it's not something a tab-completer / the analyser can help with. And third-party libraries can be very hit-or-miss with their documentation. One possible solution is to not allow inline-catches to catch any |
I'd make it Maybe change Idea: Any switch statement or expression can add one or more We can let switch expressions with only (Might want to do something with |
@lrhn:
Q: have you ever considered supporting the form |
Doesn't have to. One keyword is enough to know that a switch entry starts there, and
... use statements. That's not just while catching exceptions. Expression blocks might be the solution for this too.
Maybe! What would it do? 🤔 |
It's just a way to make it look a bit more consistent with the general practice of writing var a = switch (final v=expr) {
catch Exception e => 0,
_ => v // to avoid writing "var v = > v"
}; Just looks a bit more familiar. Or not? About "finally". It's probably not very relevant to the case. When we invoke just a single expression, it shouldn't leave any observable side effects, especially those requiring cleanup. |
@lrhn: Further. some exceptions may inherit a (hypothetical) WDYT? |
It's hypothetically possible to recognize that the handler on the stack does not catch a stack trace, and does not rethrow. If that is the case, there is no need to create a I added "hypothetically" because it doesn't work as easily with For synchronous code, all general code tends to capture stack traces, because someone might want it. Or you want to log it. Or you threw an (If I designed this from scratch today, I'd probably only have I don't think introducing special kinds of exceptions that must be handled (Java checked exceptions, basically) is going to fly. Probably doable using annotations and a lint, though. |
The use cases for "traceless" exceptions are exactly those where people are currently requesting union types (I figure this is the main use case for union types). Introducing the second color of the error-handling mechanism in the form of union types would further confuse the user, so I'm fantasizing about re-purposing the existing throw-catch mechanism for that - assuming it wouldn't incur a disproportional performance penalty associated with the creation of the stack trace. If you believe capturing stack trace is unnecessary for Exceptions, you can introduce a TracelessException which won't capture it, and use it in the above scenario. It will create a StackTrace object, but it will be empty (or contain a minimal amount of information). Then, there will be no problem with |
I think there is no Dart lint right now that would do this, but DCM has this rule for "ckeched exceptions" https://dcm.dev/docs/rules/common/handle-throwing-invocations |
This is nice |
Seems bad , as |
This is a request for Zig-style inline catching, which would allow something like the following:
Which would then obviate the need for specific APIs like:
The problem is that not everything has a premade API that returns null if an error occurred. In fact, there may be some use cases, such as config parsing, where someone may wish to retain
null
as a valid return type distinct from there having been an error. And if you're someone who cares about finality (that once a variable is set, it cannot be changed), then your options are rather limited.For example, the following is not possible:
The only way that I've found to do this is via:
Which is far from ideal.
The text was updated successfully, but these errors were encountered: