Dart Analyze logoDart Analyze/
DRT-W1286

Unreachable `catch` clause after a more general oneDRT-W1286

Major severityMajor
Bug Risk categoryBug Risk

The analyzer produces this diagnostic when a catch clause is found that can't be executed because it is after a catch clause that catches either the same type or a supertype of the clause's type. The first catch clause that matches the thrown object is selected, and the earlier clause always matches anything matchable by the highlighted clause, so the highlighted clause will never be selected.

Example

The following code produces this diagnostic:

void f() {
  try {
  } on num {
  } on int {
  }
}

Common fixes

If the clause should be selectable, then move the clause before the general clause:

void f() {
  try {
  } on int {
  } on num {
  }
}

If the clause doesn't need to be selectable, then remove it:

void f() {
  try {
  } on num {
  }
}