跳到主内容

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 文件中,将 use_rethrow_when_possible 添加到 linter > rules

analysis_options.yaml
yaml
linter:
  rules:
    - use_rethrow_when_possible

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

analysis_options.yaml
yaml
linter:
  rules:
    use_rethrow_when_possible: true