avoid-catches-without-on-clauses
Same as avoid_catches_without_on_clauses in Dart lints
- Group
- Style
- Path
lint/style/avoid-catches-without-on-clauses- Default
- Off, opt in
- Origin
- Dart lints
- Rule
- 28 of 512
Avoid catch clauses without an on type.
A bare catch (e) swallows everything, including programming errors that
should surface. Name the exceptions you can handle:
try { work(); } catch (e) { log(e); } // BAD
try { work(); } on IOException catch (e) { log(e); } // GOOD
A bare catch is allowed when its body hands the caught object on: a
rethrow (before any nested catch clause), a throw mentioning it, or
passing it to Future.error, Completer.completeError,
FlutterError.reportError, Error.throwWithStackTrace, or another call
whose resolved return type is Never. A completeError call on a receiver
whose type cannot be resolved also counts, so unknown receivers stay silent.
Invalid
import 'dart:async';
void work() {}
void log(Object e) {}
void bareCatch() {
try {
work();
} catch (e) {
log(e);
}
}
void bareCatchWithStack() {
try {
work();
} catch (e, s) {
log(s);
}
}
void throwsSomethingElse() {
try {
work();
} catch (e) {
throw StateError('failed');
}
}
void rethrowAfterNestedCatch() {
try {
work();
} catch (e) {
try {
work();
} on FormatException {
log(e);
}
rethrow;
}
}
void ignoresException() {
try {
work();
} catch (e) {
print('failed');
}
}
void completesWithOtherError(Completer<void> completer) {
try {
work();
} catch (e) {
completer.completeError(StateError('other'));
}
} work(); } catch (e) { work(); } catch (e, s) { work(); } catch (e) { work(); } catch (e) { work(); } catch (e) { work(); } catch (e) {Valid
import 'dart:async';
void work() {}
void log(Object e) {}
void withOn() {
try {
work();
} on FormatException catch (e) {
log(e);
}
}
void onWithoutCatch() {
try {
work();
} on Exception {
log('x');
}
}
void rethrows() {
try {
work();
} catch (e) {
log(e);
rethrow;
}
}
void throwsWrapped() {
try {
work();
} catch (e) {
throw StateError('wrapped: $e');
}
}
Future<void> futureError() {
try {
work();
return Future.value();
} catch (e) {
return Future.error(e);
}
}
void completer(Completer<void> completer) {
try {
work();
} catch (e, s) {
completer.completeError(e, s);
}
}
void throwWithStack() {
try {
work();
} catch (e, s) {
Error.throwWithStackTrace(e, s);
}
}
Never fail(Object error) => throw error;
void neverReturning() {
try {
work();
} catch (e) {
fail(e);
}
}
void inferredCompleterInCallback(Stream<int> events) {
final completed = Completer<Map<String, dynamic>>();
events.listen((event) async {
try {
work();
} catch (error, stack) {
// Inferred SDK receivers forward errors just like annotated receivers.
if (!completed.isCompleted) completed.completeError(error, stack);
}
});
}Configure avoid-catches-without-on-clauses
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"avoid-catches-without-on-clauses": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/avoid-catches-without-on-clauses: <reason>More style rules
- avoid-catching-errors
avoid_catching_errorsDon't explicitly catch
Erroror types that implement it. - avoid-double-and-int-checks
avoid_double_and_int_checksrecommendedAvoid checking a value for
doubleand then forint. - avoid-equals-and-hash-code-on-mutable-classes
avoid_equals_and_hash_code_on_mutable_classesFlags
==andhashCodeoverrides in classes not marked@immutable. - avoid-escaping-inner-quotes
avoid_escaping_inner_quotesFlags a string literal that escapes its own quote character when switching the outer quotes would remove every escape.
- avoid-field-initializers-in-const-classes
avoid_field_initializers_in_const_classesFlags field initializers in classes with a
constconstructor. - avoid-final-parameters
avoid_final_parametersFlags parameters declared
final. - avoid-implementing-value-types
avoid_implementing_value_typesFlags
implementsof a class that overrides==. - avoid-init-to-null
avoid_init_to_nullrecommendedFlags explicit
= nullinitializers on declarations that already default to null.