only_throw_errors
Only throw instances of classes extending either Exception or Error.
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:
dart
void throwString() {
throw 'hello world!'; // LINT
}
GOOD:
dart
void throwArgumentError() {
Error error = ArgumentError('oh!');
throw error; // OK
}
Enable
#To enable the only_throw_errors
rule,
add only_throw_errors
under linter > rules in your
analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- only_throw_errors
If you're instead using the YAML map syntax to configure linter rules,
add only_throw_errors: true
under linter > rules:
analysis_options.yaml
yaml
linter:
rules:
only_throw_errors: true
除非另有说明,文档之所提及适用于 Dart 3.7.2 版本,本页面最后更新时间: 2025-03-07。 查看文档源码 或者 报告页面问题。