avoid_ catching_ errors
Learn about the avoid_catching_errors linter rule.
Don't explicitly catch Error or types that
implement it.
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:
try {
somethingRisky();
} on Error catch(e) {
doSomething(e);
}
GOOD:
try {
somethingRisky();
} on Exception catch(e) {
doSomething(e);
}
Enable
#
To enable the avoid_catching_errors rule, add
avoid_catching_errors under
linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- avoid_catching_errors
If you're instead using the YAML map syntax to configure
linter rules, add
avoid_catching_errors: true under
linter > rules:
linter:
rules:
avoid_catching_errors: true
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.