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

Allow pattern matching on error handling #3673

Open
rubenferreira97 opened this issue Mar 26, 2024 · 0 comments
Open

Allow pattern matching on error handling #3673

rubenferreira97 opened this issue Mar 26, 2024 · 0 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@rubenferreira97
Copy link

With the recent introduction of pattern matching in Dart, it would be beneficial to extend this feature to error handling, enabling more expressive and concise error handling mechanisms.

This proposal aims to leverage pattern matching syntax for catching exceptions, providing developers with more flexibility and clarity in error handling while aiming to solve #112.

Straw man syntax:

class FooException implements Exception {
  final String msg1;
  FooException(this.msg1);
}

class BarException implements Exception {
  final String msg2;
  BarException(this.msg2);
}

void main() {
  try {
    throw FooException('Hi!');
  } catch (e case FooException() || BarException(), StackTrace s) {
    print('Exception is FooException or BarException');
  } catch (e case FooException(msg1: final m) || BarException(msg2: final m) when m.length > 0, s) {
    print(m);
  }
}

Since there are no checked exceptions in Dart, we cannot exhaustively check for exceptions.

@rubenferreira97 rubenferreira97 added the feature Proposed language feature that solves one or more problems label Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

1 participant