avoid_catching_errors
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
Unless stated otherwise, the documentation on this site reflects Dart 3.6.0. Page last updated on 2025-01-27. View source or report an issue.