Skip to content

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

Closed as not planned
@MisterMirko

Description

@MisterMirko

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).closed-duplicateClosed in favor of an existing reporttype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    Status

    Complete

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions