目錄

only_throw_errors

僅拋出繼承自 Exception 或 Error 類別的實例。

此規則自 Dart 2.0 起可用。

詳細資訊

#

應該只拋出繼承 dart.core.Errordart.core.Exception 的類別實例。

拋出未繼承 ErrorException 的實例是不好的做法;這樣做通常是為了某種應該更徹底實現的東西而採取的 hack。

不良範例

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