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
文件中,将 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