avoid_catching_errors
Don't explicitly catch Error
or types that implement it.
This rule is available as of Dart 2.0.
Details
#DON'T explicitly catch Error
or types that implement it.
Errors differ from Exceptions in that Errors can be analyzed and prevented prior to runtime. It should almost never be necessary to catch an error at runtime.
BAD:
dart
try {
somethingRisky();
} on Error catch(e) {
doSomething(e);
}
GOOD:
dart
try {
somethingRisky();
} on Exception catch(e) {
doSomething(e);
}
Usage
#To enable the avoid_catching_errors
rule,
add avoid_catching_errors
under linter > rules in your
analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- avoid_catching_errors
除非另有说明,文档之所提及适用于 Dart 3.5.4 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。