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