Skip to content

catch_only_rethrows

Reports a last catch clause whose body is rethrow; alone. Delete it.

Tier
advisory
Scope
Dart
Lowest preset
recommended
Fix
deletes the clause, or writes the body in place of the try

The last catch clause of a try whose body is rethrow; alone. A finally makes no difference.

It does not report such a clause before another one: there it keeps the later clauses from catching that error.

The fix deletes the clause. Where the clause is the only one and there is no finally, it writes the statements of the body in place of the try, where the try stands among the statements of a block, its body declares nothing on its own level and no string of it spans lines. No fix is offered where it would delete a comment.

The error goes on with its stack trace exactly as it would without the clause.

Violating
try {
load();
} on FormatException {
report();
} catch (error) {
rethrow;
}
Corrected
try {
load();
} on FormatException {
report();
}

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

Write why on the line directly above the suppression:

// The clause marks where the next release handles this error.
// ignore: assay/catch_only_rethrows

// ignore_for_file: assay/catch_only_rethrows silences it in a whole file, and // ignore: catch_only_rethrows without the prefix works too. See Suppressions.