跳到主要内容

use_rethrow_when_possible

稳定版
推荐
修复可用

使用 rethrow 重新抛出捕获的异常。

详情

#

来自 Effective Dart

务必 使用 rethrow 重新抛出捕获的异常。

由于 Dart 提供了 rethrow 作为一项功能,因此应使用它来提高简洁性和可读性。

不良示例

dart
try {
  somethingRisky();
} catch(e) {
  if (!canHandle(e)) throw e;
  handle(e);
}

良好示例

dart
try {
  somethingRisky();
} catch(e) {
  if (!canHandle(e)) rethrow;
  handle(e);
}

启用

#

要启用 use_rethrow_when_possible 规则,请在您的 analysis_options.yaml 文件中的 linter > rules 下添加 use_rethrow_when_possible

analysis_options.yaml
yaml
linter:
  rules:
    - use_rethrow_when_possible

如果您改为使用 YAML 映射语法来配置 linter 规则,请在 linter > rules 下添加 use_rethrow_when_possible: true

analysis_options.yaml
yaml
linter:
  rules:
    use_rethrow_when_possible: true