avoid-catching-errors
Same as avoid_catching_errors in Dart lints
- Group
- Style
- Path
lint/style/avoid-catching-errors- Default
- Off, opt in
- Origin
- Dart lints
- Rule
- 29 of 512
Don't explicitly catch Error or types that implement it.
An Error signals a programming bug that should crash and be fixed, not be
handled at runtime:
try { work(); } on StateError catch (e) { recover(); } // BAD
try { work(); } on FormatException catch (e) { recover(); } // GOOD
Only on types whose resolved supertype chain provably reaches dart:core
Error are reported.
Invalid
void work() {}
class MyError extends Error {}
class MyStateError extends StateError {
MyStateError(super.message);
}
void catchesError() {
try {
work();
} on Error catch (e) {
print(e);
}
}
void catchesStateError() {
try {
work();
} on StateError catch (e) {
print(e);
}
}
void catchesArgumentError() {
try {
work();
} on ArgumentError {
print('bad argument');
}
}
void catchesCustomError() {
try {
work();
} on MyError catch (e) {
print(e);
}
}
void catchesNestedCustomError() {
try {
work();
} on MyStateError catch (e) {
print(e);
}
}
void catchesTypeError() {
try {
work();
} on TypeError catch (e) {
print(e);
}
}
extension type WrappedStateError(StateError error) implements StateError {}
extension type WrappedError(Error error) implements Object {}
void catchesWrappedErrors() {
try {} on WrappedStateError {}
try {} on WrappedError catch (_) {}
} work(); } on Error catch (e) { work(); } on StateError catch (e) { work(); } on ArgumentError { work(); } on MyError catch (e) { work(); } on MyStateError catch (e) { work(); } on TypeError catch (e) {void catchesWrappedErrors() { try {} on WrappedStateError {} try {} on WrappedStateError {} try {} on WrappedError catch (_) {}Valid
void work() {}
class MyException implements Exception {}
class Unrelated {}
void catchesException() {
try {
work();
} on Exception catch (e) {
print(e);
}
}
void catchesFormatException() {
try {
work();
} on FormatException catch (e) {
print(e);
}
}
void catchesCustomException() {
try {
work();
} on MyException catch (e) {
print(e);
}
}
void catchesUnrelated() {
try {
work();
} on Unrelated catch (e) {
print(e);
}
}
void catchesObject() {
try {
work();
} on Object catch (e) {
print(e);
}
}
void bareCatch() {
try {
work();
} catch (e) {
print(e);
}
}Configure avoid-catching-errors
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"avoid-catching-errors": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/avoid-catching-errors: <reason>More style rules
- 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. - avoid-js-rounded-ints
avoid_js_rounded_intsrecommendedAvoid integer literals that JavaScript would round.