empty-catches

Same as empty_catches in Dart lints

Path
lint/suspicious/empty-catches
Default
On (recommended)
Origin
Dart lints
Rule
229 of 512
recommended

Flags empty catch blocks.

A catch clause with no body silently swallows the exception, hiding failures that should be handled, logged, or rethrown and making bugs far harder to diagnose. Handle the error, or at minimum log it, rather than discarding it. Two escape hatches match the official lint: a catch whose body contains a comment (the emptiness is deliberate and documented), and a catch that binds its exception to _, an explicit "I am ignoring this" marker.

This includes catch clauses inside anonymous functions.

Invalid

example.dartdart
void f1() {
  try {
    doThing();
  } catch (e) {}
}

void f2() {
  try {
    doThing();
  } on Exception catch (e) {}
}

void f3() {
  try {
    doThing();
  } catch (e, st) {}
}

void f4() {
  try {
    doThing();
  } on StateError {}
}

void f5() {
  try {
    doThing();
  } catch (error) {}
}

void f6() {
  try {
    doThing();
  } catch (e) {
  }
}
Handle the exception, add a comment, or name it `_` instead of leaving this catch block empty.
3 doThing();
4 } catch (e) {}
Handle the exception, add a comment, or name it `_` instead of leaving this catch block empty.
9 doThing();
10 } on Exception catch (e) {}
Handle the exception, add a comment, or name it `_` instead of leaving this catch block empty.
15 doThing();
16 } catch (e, st) {}
Handle the exception, add a comment, or name it `_` instead of leaving this catch block empty.
21 doThing();
22 } on StateError {}
Handle the exception, add a comment, or name it `_` instead of leaving this catch block empty.
27 doThing();
28 } catch (error) {}
Handle the exception, add a comment, or name it `_` instead of leaving this catch block empty.
34 } catch (e) {
35 }

Valid

example.dartdart
void g1() {
  try {
    doThing();
  } catch (e) {
    print(e);
  }
}

void g2() {
  try {
    doThing();
  } catch (_) {}
}

void g3() {
  try {
    doThing();
  } catch (e) {
    // Failure is expected and intentionally ignored here.
  }
}

void g4() {
  try {
    doThing();
  } on Exception catch (e) {
    handle(e);
  }
}

void g5() {
  try {
    doThing();
  } on StateError catch (_) {}
}

void g6() {
  try {
    doThing();
  } catch (e, st) {
    print('$e $st');
  }
}

String documentedInterpolation() => '${(() {
  try {
    doThing();
  } catch (error) { /* Deliberately ignore this } and { failure. */ }
  return '';
})()}';

Configure empty-catches

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "empty-catches": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/suspicious/empty-catches: <reason>
  • empty-statementsempty_statementsrecommended

    Flags stray empty statements (a lone ;).

  • no-duplicate-case-valuesno_duplicate_case_valuesrecommended

    Flags semantically equal constant values within one switch.

  • no-empty-blockno-empty-block

    Flags empty blocks ({}).

  • no-equal-argumentsno-equal-arguments

    Flags an argument passed more than once in the same invocation.

  • no-equal-then-elseno-equal-then-elserecommended

    Flags an if/else or ternary whose two branches are identical.

  • no-magic-string

    Extract inline string literals into named variables or constants.

  • no-self-comparisonsno_self_comparisonsrecommended

    Flags proven self-comparisons of side-effect-free scalar values.

  • no-wildcard-variable-usesno_wildcard_variable_usesrecommended

    Flags references to a wildcard variable or parameter (a name made solely of underscores, such as _ or __).