-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[breaking change] Extend function definition with "throws exception" #56926
Comments
Summary: This issue proposes adding a "throws exception" clause to function definitions in Dart to explicitly signal that a function might throw an exception. This would improve code clarity and stability by making potential exceptions more visible to developers. |
Duplicate of dart-lang/language#984 |
This would only work for exceptions, not That's also a problem. Knowing that a function may throw an exception does not tell you which reception it throws. All in all, Java-stole "checked exceptions" has been suggested before (fx dart-lang/language#984). It's unlikely that Dart will go that way, for the same reasons that no post-Java language has cooled that particular feature. It's not worth the overhead it causes. |
I read through the mentioned issues and I agree that it may not become a feature of the dart language. But the issue remains. Is there some work going on to adress it? If not maybe at least some smaller things could be done? #58232 |
Change Intent
Enable an optional way for communicating that exceptions can happen by extending the function definition with the phrase "throws exception":
Currently:
An IDE might propose a Quick-Fix when "throws exception" is not present in the function definition although the function might throw an exception.
(Though I personally prefer the sound of "raises exception" but "throws exception" is probably better suited since the keyword
throw
is used in the dart language.)Justification
Without any digging there is no way of knowing whether a function might throw an error or not. Having (1) a program running unexpectedly into an error or (2) encapsule "just in case" everything in a try-catch block and say "sorry, an unexpected error has occured" is fatal in the first case and is at least no good practice in the second case.
Especially for new programmers or even experienced ones using new packages this would be really helpful.
Impact
Anyone could see at a glance if a function might throw an exception. Having this information would massively improve stability and usabilty of programs in general. Making this feature optional also means that any progammer can decide to use it. I would suggest to make the IDE point out a missing "throws exception" and offer a quick fix.
Mitigation
No response
Change Timeline
No response
Associated CLs
No response
The text was updated successfully, but these errors were encountered: