跳到主要内容

only_throw_errors

稳定

仅抛出扩展 Exception 或 Error 类的实例。

详情

#

应该 仅抛出扩展 dart.core.Errordart.core.Exception 类的实例。

抛出不扩展 ErrorException 的实例是不良实践;这样做通常是一种权宜之计,应该更彻底地实现。

不良示例

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