Catch multiple specific exception types in Dart with one catch expression

混江龙づ霸主 提交于 2020-04-09 18:08:54

问题


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

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

But is there a way to catch multiple specific exception types with on line instead of using multiple catch statements?


回答1:


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
}


来源:https://stackoverflow.com/questions/32627717/catch-multiple-specific-exception-types-in-dart-with-one-catch-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!