avoid_only_rethrow
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.
Why use this rule
Section titled “Why use this rule”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();}Turning this rule off
Section titled “Turning this rule off”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:
rules: avoid_only_rethrow: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Options
Section titled “Options”many_lints: rules: avoid_only_rethrow: ignore_typed_catches: truerules: 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.
Related rules
Section titled “Related rules”avoid_cascade_after_if_null— Detect cascades after if-null operators without parentheses.avoid_collapsible_if— Merge nested if statements with &&.avoid_constant_conditions— Detect comparisons where both sides are constants.avoid_constant_switches— Detect switch statements on constant expressions.