Skip to main content

avoid-throw-in-catch-block

added in: 1.6.0
Free+
preset: recommended

Warns when a throw expression is called inside a catch block

Calling throw inside a catch block loses the original stack trace and the original exception.

Since Dart 2.16 you can use Error.throwWithStackTrace to avoid loosing the original stack trace.

Example

❌ Bad:

void repository() {
try {
networkDataProvider();
} on Object {
throw RepositoryException(); // LINT
}
}

✅ Good:

void repository() {
try {
networkDataProvider();
} catch (_, stack) {
Error.throwWithStackTrace(RepositoryException(), stack);
}
}