avoid-only-rethrow
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;
}
}