avoid_swallowed_errors
Reports a catch clause that does nothing with the error it caught. Rethrow it, log it, or carry it in what you return.
- Tier
- blocking
- Scope
- Dart
- Fix
- none
What it reports
Section titled “What it reports”- A clause whose body is empty or holds only comments, whatever it catches:
catch (error) {},on FormatException { /* expected */ }. A body of only empty statements and empty blocks counts as empty. - A clause that catches every error and leaves without using it. It catches every error when it names no type, or names
Object,Exception,Errorordynamic. It leaves without using the error when its body neither rethrows nor mentions the error or its stack trace anywhere. These are violations:catch (error) { return null; },catch (_) { print('Saving failed.'); },on Exception { failed = true; },catch (error) { throw StateError('Loading failed.'); }.
The violation is at the head of the clause, such as catch (error), on FormatException or on Exception catch (error, stackTrace).
It does not report:
- a clause that rethrows:
catch (error) { cleanUp(); rethrow; }; - a clause that passes the error to a logging or reporting call:
log.warning('Saving failed.', error, stackTrace),print('Saving failed: $error'); - a clause that uses the error in the value it returns or throws:
return Failure(error),throw StorageFailure('Loading failed: $error'); - a clause that keeps or tests the error:
lastError = error,if (error is TimeoutException) …; - a clause on one specific type of error with code in its body, whether or not that code uses the error:
on FormatException { return null; }. Naming the type and writing what happens then is a decision about that error, not a way around the rule.
A swallowed error is a failure that leaves no trace. The operation did not happen, the program goes on as if it had, and whoever looks into the wrong result later has nothing to start from: no log line, no error value, no stack trace.
A catch clause is the shortest way to make a failing call stop failing. A clause that catches everything is the worst form: it also hides the errors nobody thought of, a null value or a wrong index among them.
Doing something with the error keeps the failure visible. Rethrowing hands it to a caller who can act, logging records it, and carrying it in the returned or thrown value shows the next reader the cause.
The SDK lint empty_catches also reports an empty catch block, so both report that place; both stay. This rule goes further: it reports an empty block that holds a comment or whose error is named _, which empty_catches accepts, and a body with code that catches everything and drops the error.
Example
Section titled “Example”Future<Settings> loadSettings(File file) async { try { return Settings.parse(await file.readAsString()); } catch (error) { return Settings.defaults(); }}Future<Settings> loadSettings(File file) async { try { return Settings.parse(await file.readAsString()); } on FileSystemException catch (error, stackTrace) { log.warning('No settings file, using the defaults.', error, stackTrace); return Settings.defaults(); }}Other ways to do something with the error, depending on who can act on it:
- hand it to the caller after local work:
catch (error) { await rollBack(); rethrow; }; - carry it in the value:
return Result.failure(error); - carry it in a new error:
throw SettingsFailure('Cannot read ${file.path}: $error'); - catch nothing: delete the
trywhen no error is expected at that place.
Rule parameters
Section titled “Rule parameters”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. |
assay: rules: avoid_swallowed_errors: exclude: ["tool/**"]Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression, which goes above the line of the clause’s head:
// Closing is best effort here: the socket may already be gone.// ignore: assay/avoid_swallowed_errors// ignore_for_file: assay/avoid_swallowed_errors silences it in a whole file, and // ignore: avoid_swallowed_errors without the prefix works too. See Suppressions.
A comment inside the body is not a suppression: it explains the clause to a reader and leaves the violation in place.