Skip to content
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

Closed
Mirko607 opened this issue Oct 21, 2024 · 4 comments
Closed

[breaking change] Extend function definition with "throws exception" #56926

Mirko607 opened this issue Oct 21, 2024 · 4 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug

Comments

@Mirko607
Copy link

Change Intent

Enable an optional way for communicating that exceptions can happen by extending the function definition with the phrase "throws exception":

// Signaling that this function might throw an exception. 
// Tooltips in IDEs coudl be extended to highlight this. 
void myFunc() throws exception { 
    throw Exception("something went wrong");
}

Currently:

void myFunc() { //no signaling whatsoever that this function might throw an exception
    throw Exception("something went wrong");
}

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

@Mirko607 Mirko607 added the breaking-change-request This tracks requests for feedback on breaking changes label Oct 21, 2024
@dart-github-bot
Copy link
Collaborator

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.

@dart-github-bot dart-github-bot added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug labels Oct 21, 2024
@lrhn lrhn removed the breaking-change-request This tracks requests for feedback on breaking changes label Oct 21, 2024
@mraleph
Copy link
Member

mraleph commented Oct 21, 2024

Duplicate of dart-lang/language#984

@mraleph mraleph marked this as a duplicate of dart-lang/language#984 Oct 21, 2024
@mraleph mraleph closed this as completed Oct 21, 2024
@github-project-automation github-project-automation bot moved this from In review to Complete in Breaking Changes Oct 21, 2024
@lrhn
Copy link
Member

lrhn commented Oct 21, 2024

This would only work for exceptions, not Errors, since every function might throw an error (if you include StackOverflowError). So the syntax is fitting, if verbose. Just throws would be enough.

That's also a problem. Knowing that a function may throw an exception does not tell you which reception it throws.
You're discouraged from just catching Exception, since the purpose of exceptions is to be caught and handled, and you can't properly handle an exception if you don't know what it represents.
Satisfying a throws by catching the wrong exception doesn't actually provide any safety.

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.

@lrhn lrhn added closed-duplicate Closed in favor of an existing report and removed triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Oct 21, 2024
@lrhn lrhn closed this as not planned Won't fix, can't repro, duplicate, stale Oct 21, 2024
@Mirko607
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug
Projects
Status: Complete
Development

No branches or pull requests

6 participants