Skip to content

avoid_only_rethrow

v0.4.0 Warning Fix Control 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

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();
}

To disable this rule:

plugins:
many_lints:
diagnostics:
avoid_only_rethrow: false