使用一个catch表达式在Dart中捕获多种特定的异常类型 [英] Catch multiple specific exception types in Dart with one catch expression

查看:282
本文介绍了使用一个catch表达式在Dart中捕获多种特定的异常类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用以下命令在dart中捕获特定的异常类型:

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

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

但是有一种方法可以在线捕获多个特定的异常类型,而不是使用多个catch语句吗?

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

推荐答案

您只能在xxx catch(e){行的每个中指定一种类型,或者使用
catch(e)捕获所有(剩余的-见下文)异常类型。
on 之后的类型用作 catch(e)的参数类型。

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
}

这篇关于使用一个catch表达式在Dart中捕获多种特定的异常类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆