avoid-catching-errors

Same as avoid_catching_errors in Dart lints

Group
Style
Path
lint/style/avoid-catching-errors
Default
Off, opt in
Origin
Dart lints
Rule
29 of 512

Don't explicitly catch Error or types that implement it.

An Error signals a programming bug that should crash and be fixed, not be handled at runtime:

try { work(); } on StateError catch (e) { recover(); } // BAD
try { work(); } on FormatException catch (e) { recover(); } // GOOD

Only on types whose resolved supertype chain provably reaches dart:core Error are reported.

Invalid

example.dartdart
void work() {}

class MyError extends Error {}

class MyStateError extends StateError {
  MyStateError(super.message);
}

void catchesError() {
  try {
    work();
  } on Error catch (e) {
    print(e);
  }
}

void catchesStateError() {
  try {
    work();
  } on StateError catch (e) {
    print(e);
  }
}

void catchesArgumentError() {
  try {
    work();
  } on ArgumentError {
    print('bad argument');
  }
}

void catchesCustomError() {
  try {
    work();
  } on MyError catch (e) {
    print(e);
  }
}

void catchesNestedCustomError() {
  try {
    work();
  } on MyStateError catch (e) {
    print(e);
  }
}

void catchesTypeError() {
  try {
    work();
  } on TypeError catch (e) {
    print(e);
  }
}

extension type WrappedStateError(StateError error) implements StateError {}
extension type WrappedError(Error error) implements Object {}

void catchesWrappedErrors() {
  try {} on WrappedStateError {}
  try {} on WrappedError catch (_) {}
}
The type `Error` should not be caught.
11 work();
12 } on Error catch (e) {
The type `StateError` should not be caught because it is a subclass of `Error`.
19 work();
20 } on StateError catch (e) {
The type `ArgumentError` should not be caught because it is a subclass of `Error`.
27 work();
28 } on ArgumentError {
The type `MyError` should not be caught because it is a subclass of `Error`.
35 work();
36 } on MyError catch (e) {
The type `MyStateError` should not be caught because it is a subclass of `Error`.
43 work();
44 } on MyStateError catch (e) {
The type `TypeError` should not be caught because it is a subclass of `Error`.
51 work();
52 } on TypeError catch (e) {
The type `WrappedStateError` should not be caught because it is a subclass of `Error`.
60void catchesWrappedErrors() {
61 try {} on WrappedStateError {}
The type `WrappedError` should not be caught because it is a subclass of `Error`.
61 try {} on WrappedStateError {}
62 try {} on WrappedError catch (_) {}

Valid

example.dartdart
void work() {}

class MyException implements Exception {}

class Unrelated {}

void catchesException() {
  try {
    work();
  } on Exception catch (e) {
    print(e);
  }
}

void catchesFormatException() {
  try {
    work();
  } on FormatException catch (e) {
    print(e);
  }
}

void catchesCustomException() {
  try {
    work();
  } on MyException catch (e) {
    print(e);
  }
}

void catchesUnrelated() {
  try {
    work();
  } on Unrelated catch (e) {
    print(e);
  }
}

void catchesObject() {
  try {
    work();
  } on Object catch (e) {
    print(e);
  }
}

void bareCatch() {
  try {
    work();
  } catch (e) {
    print(e);
  }
}

Configure avoid-catching-errors

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-catching-errors": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/avoid-catching-errors: <reason>