Skip to content

proposal: strict_exceptions (safe/unsafe concept to make Dart more predictable) #58934

Closed as not planned
@bernaferrari

Description

@bernaferrari

strict_exceptions

Details

Disclaimer: I know the odds are against this issue, but I really like this idea and would love to see it at least being considered for 10 seconds.

I love Rust safeness. I dislike when someone calls firstWhere and it throws. Or when someone does http.get and it fails (by throwing an exception). Or when someone calls json.decode and something bad happens. These errors usually happen on runtime and are unpredictable. Uber had an issue where something that might never happen happened when a server call went rogue. Certain kind of runtime issues could be avoided in Dart, I believe.

Maaaaybe, Dart could have an annotation like @unsafe that the analyzer uses to check if certain code can or cannot crash. It wouldn't affect anything else, but it would surely be a helper for people that want a stricter code with greater warranties that it won't misbehave.

JavaScript has strict mode, which eliminates silent errors. I wish Dart had a similar feature (although this would be way more strict than JS).

With sealed classes, most of these weird exception situations (like http.get) could be wrapped into a Result: Success | Error scenario, like Kotlin. Therefore, unpredictable places throwing an exception could be an even stronger motivation for my strict mode proposal.

Biggest challenge is figuring out how plug-ins would work with this lint when not opted in.

Kind

Guard against runtime unpredictable exceptions. Allow the user to have a predictable execution.

Bad Examples

void searchForSomething(List<String> list) {
  list.firstWhere(...); // uh oh! This might throw an exception!
}

void getDocument() {
  Firebase.getDocument(); // This library doesn't opt into strict mode. You should add a try/except around this method or @unsafe to the function.
}

Good Examples

@unsafe
void searchForSomething(List<String> list) {
  list.firstWhere(...); // Ok, the annotation says this is ok.
}

void searchForSomething(List<String> list) {
  try {
      list.firstWhere(...); // Inside try/catch. Perfect.
  } catch () {
    // ...
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions