dart-lang / dart-lang/language

Allow pattern matching on error handling

Open
#3,673 0 comments 20 reactions 0 assignees View on GitHub
feature
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

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:
```dart
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.

Contributor guide

Open the contributing guide

Research direction

No files, tests, or entry points are named. Start by reviewing the existing Dart pattern-matching and exception-handling language specifications, then compare them with the straw-man syntax in this issue. Done means reaching and documenting an accepted design for pattern matching in exception handlers, including its interaction with non-exhaustive exceptions.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.