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);
}
}
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.