Rules / Suspicious / empty-catches

empty-catches

lint/suspicious/empty-catches
recommended
Same as empty_catches · Dart lints

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.

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) {
  }
}
Empty catch block — handle the exception, add a comment, or name it `_`
3 doThing();
4 } catch (e) {}
Empty catch block — handle the exception, add a comment, or name it `_`
9 doThing();
10 } on Exception catch (e) {}
Empty catch block — handle the exception, add a comment, or name it `_`
15 doThing();
16 } catch (e, st) {}
Empty catch block — handle the exception, add a comment, or name it `_`
21 doThing();
22 } on StateError {}
Empty catch block — handle the exception, add a comment, or name it `_`
27 doThing();
28 } catch (error) {}
Empty catch block — handle the exception, add a comment, or name it `_`
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');
  }
}

How to configure

Set the severity of empty-catches in your falcon.json:

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