Skip to main content

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:

dart
void f() {
  try {
    int.parse('?');
  } catch (e) {
    print(e);
  }
}

Common fixes

#

Add an on clause to specify the type of exception to catch:

dart
void f() {
  try {
    int.parse('?');
  } on FormatException catch (e) {
    print(e);
  }
}