Skip to main content

avoid-only-rethrow

added in: 1.18.0
Pro+
preset: recommended

Warns when a catch clause has only a rethrow expression.

Catch clauses with only the rethrow expression should either have some additional code to handle some type of exceptions or can be simply removed since they are redundant.

Example

❌ Bad:

void main() {
try {
...
} on Object catch (error) {
rethrow; // LINT: Avoid catch clauses with only 'rethrow'.
}
}

✅ Good:

void main() {
try {
...
} on Object catch (error) {
if (error is Exception) {
// Correct, handles some type of exceptions
return;
}

rethrow;
}
}

Additional Resources