avoid_catching_errors

Stable

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:

dart
try {
  somethingRisky();
} on Error catch(e) {
  doSomething(e);
}

GOOD:

dart
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:

analysis_options.yaml
yaml
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:

analysis_options.yaml
yaml
linter:
  rules:
    avoid_catching_errors: true