Skip to content

Allow pattern matching on error handling #3673

Open
@rubenferreira97

Description

@rubenferreira97

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions