only-throw-errors
Same as only_throw_errors in Dart lints
- Group
- Style
- Path
lint/style/only-throw-errors- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 305 of 512
Only throw instances of classes that extend Exception or Error.
Throwing arbitrary objects breaks on Exception / on Error handlers and
loses the conventions tooling relies on:
throw 'Something went wrong'; // BAD
throw FormatException('Something went wrong'); // GOOD
Literals are always reported. Other thrown values are reported only when
their resolved type provably implements neither Exception nor Error;
dynamic, Never, and anything unresolved stay silent. Function and
record values cannot implement either throwable interface and are reported.
Resolved types include promotions on the current control-flow path, and
extension types are checked through their representation types.
Invalid
class NotAnException {}
class Wrapper<T> {
T value;
Wrapper(this.value);
}
void throwsString() {
throw 'Something went wrong';
}
void throwsInt() {
throw 42;
}
void throwsList() {
throw ['a', 'b'];
}
void throwsInterpolation(int code) {
throw 'code $code';
}
void throwsCustom() {
throw NotAnException();
}
int throwsInExpression(int? value) => value ?? (throw 'missing');
void throwsLocal() {
final error = NotAnException();
throw error;
}
void throwsObject(Object error) {
throw error;
}
void throwsFunction(void Function() callback) {
throw callback;
}
void throwsRecord((int, String) value) {
throw value;
}
extension type ObjectValue(Object value) implements Object {}
void throwsObjectValue() {
throw ObjectValue(Object());
}
// Declare the getter locally: this corpus runs without SDK member signatures.
// The inherited List.first cases live in the external-source inference tests.
class CollectedErrors {
final _values = <Object>[];
Object get first => _values.first;
void add(Object error) => _values.add(error);
}
void throwsFirst(CollectedErrors errors) {
throw errors.first;
}
void throwsCollectedErrors() {
final errors = CollectedErrors();
try {
throw Error();
} catch (error) {
errors.add(error);
}
throw errors.first;
}void throwsString() { throw 'Something went wrong';void throwsInt() { throw 42;void throwsList() { throw ['a', 'b'];void throwsInterpolation(int code) { throw 'code $code';void throwsCustom() { throw NotAnException(); int throwsInExpression(int? value) => value ?? (throw 'missing'); final error = NotAnException(); throw error;void throwsObject(Object error) { throw error;void throwsFunction(void Function() callback) { throw callback;void throwsRecord((int, String) value) { throw value;void throwsObjectValue() { throw ObjectValue(Object());void throwsFirst(CollectedErrors errors) { throw errors.first; } throw errors.first;Valid
class MyException implements Exception {}
class MyError extends Error {}
void throwsException() {
throw Exception('Something went wrong');
}
void throwsFormatException() {
throw FormatException('bad input');
}
void throwsStateError() {
throw StateError('bad state');
}
void throwsCustomException() {
throw MyException();
}
void throwsCustomError() {
throw MyError();
}
void throwsDynamic(dynamic value) {
throw value;
}
void throwsBounded<T extends Exception>(T error) {
throw error;
}
Never fail() => throw UnsupportedError('nope');
int throwsInExpression(int? value) => value ?? (throw ArgumentError('missing'));
// Interface checks use the representation, even when only Object is implemented.
extension type ErrorValue(Error value) implements Object {}
extension type ExceptionValue(Exception value) implements Object {}
extension type NestedErrorValue(ErrorValue value) implements Object {}
extension type GenericErrorValue<T extends Error>(T value) implements Object {}
void throwsErrorValue() {
throw ErrorValue(Error());
}
void throwsExceptionValue() {
throw ExceptionValue(Exception());
}
void throwsNestedErrorValue() {
throw NestedErrorValue(ErrorValue(Error()));
}
void throwsGenericErrorValue() {
throw GenericErrorValue<StateError>(StateError('bad state'));
}
void throwsBoundedErrorValue<T extends ErrorValue>(T value) {
throw value;
}Configure only-throw-errors
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"only-throw-errors": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/only-throw-errors: <reason>More style rules
- package-names
package_namesrecommendedcross-fileRequire package names to be valid
lower_case_with_underscoresidentifiers. - parameter-assignments
parameter_assignmentsDon't reassign parameters of functions, methods, or constructors.
- parameters-ordering
Requires named parameters to follow the explicitly configured alphabetical order.
- pattern-fields-ordering
Flags named object and record pattern fields that violate the configured order.
- prefer-adjacent-string-concatenation
prefer_adjacent_string_concatenationrecommendedFlags two string literals joined with the
+operator. - prefer-align-over-containerflutter
Prefers
Alignover aContainerused only foralignment. - prefer-asserts-in-initializer-lists
prefer_asserts_in_initializer_listsFlags leading constructor-body asserts that could live in the initializer list.
- prefer-asserts-with-message
prefer_asserts_with_messageFlags
assertstatements and assert initializers that have no message.