only_throw_errors
仅抛出扩展 Exception 或 Error 类的实例。
详情
#应该 仅抛出扩展 dart.core.Error
或 dart.core.Exception
类的实例。
抛出不扩展 Error
或 Exception
的实例是不良实践;这样做通常是一种权宜之计,应该更彻底地实现。
不良示例
dart
void throwString() {
throw 'hello world!'; // LINT
}
良好示例
dart
void throwArgumentError() {
Error error = ArgumentError('oh!');
throw error; // OK
}
启用
#要启用 only_throw_errors
规则,请在您的 analysis_options.yaml
文件中的 linter > rules 下添加 only_throw_errors
analysis_options.yaml
yaml
linter:
rules:
- only_throw_errors
如果您改为使用 YAML 映射语法来配置 linter 规则,请在 linter > rules 下添加 only_throw_errors: true
analysis_options.yaml
yaml
linter:
rules:
only_throw_errors: true
除非另有说明,否则本网站上的文档反映的是 Dart 3.7.1 版本。页面上次更新于 2025-03-07。 查看源代码 或报告问题。