Catch multiple specific exception types in Dart with one catch expression

前端 未结 2 1413
南旧
南旧 2021-02-12 20:39

I know I can catch a specific Exception type in dart with the following:

try {
  ...
} on SpecificException catch(e) {
  ...
}

But is there a w

相关标签:
2条回答
  • 2021-02-12 20:49

    No, there isn't, but you can do this:

    try {
      ...
    } catch (e) {
      if (e is A || e is B {
        ...
      } else {
        rethrow;
      }
    }
    
    0 讨论(0)
  • 2021-02-12 20:59

    You can only specify one type per on xxx catch(e) { line or alternatively use catch(e) to catch all (remaining - see below) exception types. The type after on is used as type for the parameter to catch(e). Having a set of types for this parameter wouldn't work out well.

    try {
      ...
    } on A catch(e) {
      ...
    } on B catch(e) {
      ...
    } catch(e) { // everything else
    }
    
    0 讨论(0)
提交回复
热议问题