Skip to content

avoid_only_rethrow

v0.4.0WarningFixConfigurableControl Flow

Warns when a catch clause contains only a rethrow statement. Such catch clauses are completely redundant — they catch an exception only to immediately rethrow it, adding no value. The entire try-catch block can be removed.

A catch clause that only rethrows does not handle, log, or transform the exception in any way. It adds indentation and visual noise without changing behavior. Removing the redundant try-catch makes the code simpler and communicates that no error handling is happening at this level.

See also: Exceptions | Dart lint: use_rethrow_when_possible

void bad() {
// Redundant catch clause
try {
doSomething();
} catch (e) {
rethrow;
}
// Same with typed on clause
try {
doSomething();
} on Exception {
rethrow;
}
// With stack trace parameter, still redundant
try {
doSomething();
} catch (e, s) {
rethrow;
}
}
void good() {
// Logging before rethrowing is meaningful
try {
doSomething();
} catch (e) {
print('Error: $e');
rethrow;
}
// Conditional rethrow with handling
try {
doSomething();
} catch (e) {
if (e is FormatException) {
handleFormat(e);
return;
}
rethrow;
}
// No try-catch needed at all if you're just rethrowing
doSomething();
}

This rule is in the recommended preset, so it is on with preset: recommended or preset: opinionated. Add it to preset: core with avoid_only_rethrow: true.

To turn it off:

many_lints.yaml
rules:
avoid_only_rethrow: false

To keep the rule on but skip certain paths, use per-rule exclude.

analysis_options.yaml
many_lints:
rules:
avoid_only_rethrow:
ignore_typed_catches: true
Option Type Default Description
ignore_typed_catches bool false Only report untyped catch (e) clauses, leaving on SomeError catch (e) { rethrow; } alone

A typed clause narrows which exceptions propagate, so it is not always redundant — enable this option if your codebase uses that pattern deliberately.