use_rethrow_when_possible
使用 rethrow 重新抛出捕获的异常。
详情
#务必 使用 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
除非另有说明,否则本网站上的文档反映了 Dart 3.7.1 版本。页面上次更新于 2025-03-07。 查看源代码 或报告问题。