only_throw_errors
Only throw instances of classes extending either Exception or Error.
This rule is available as of Dart 2.0.
Details
#DO throw only instances of classes that extend dart.core.Error
or dart.core.Exception
.
Throwing instances that do not extend Error
or Exception
is a bad practice; doing this is usually a hack for something that should be implemented more thoroughly.
BAD:
void throwString() {
throw 'hello world!'; // LINT
}
GOOD:
void throwArgumentError() {
Error error = ArgumentError('oh!');
throw error; // OK
}
Usage
#To enable the only_throw_errors
rule, add only_throw_errors
under linter > rules in your analysis_options.yaml
file:
linter:
rules:
- only_throw_errors
Unless stated otherwise, the documentation on this site reflects Dart 3.6.0. Page last updated on 2024-07-03. View source or report an issue.