avoid_ catches_ without_ on_ clauses
Details about the 'avoid_catches_without_on_clauses' diagnostic produced by the Dart analyzer.
Catch clause should use 'on' to specify the type of exception being caught.
Description
#
The analyzer produces this diagnostic when a catch clause lacks an
on clause to specify the exception type. Catching all exceptions
can mask unexpected errors and make debugging more difficult.
Example
#
The following code produces this diagnostic because the catch clause
has no on clause to specify the exception type:
void f() {
try {
int.parse('?');
} catch (e) {
print(e);
}
}
Common fixes
#Add an on clause to specify the type of exception to catch:
void f() {
try {
int.parse('?');
} on FormatException catch (e) {
print(e);
}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.11.0. Report an issue.