empty_catches
避免空的 catch 代码块。
详情
#避免 空的 catch 代码块。
通常来说,应该避免空的 catch 代码块。在需要使用空 catch 代码块的情况下,应该提供注释来解释为什么要捕获和忽略异常。或者,可以将异常标识符命名为下划线 (例如,_
) 以表明我们打算跳过它。
不良示例
dart
try {
...
} catch(exception) { }
良好示例
dart
try {
...
} catch(e) {
// ignored, really.
}
// Alternatively:
try {
...
} catch(_) { }
// Better still:
try {
...
} catch(e) {
doSomething(e);
}
启用
#要启用 empty_catches
规则,请在你的 analysis_options.yaml
文件中的 linter > rules 下添加 empty_catches
analysis_options.yaml
yaml
linter:
rules:
- empty_catches
如果你改为使用 YAML 映射语法来配置 linter 规则,请在 linter > rules 下添加 empty_catches: true
analysis_options.yaml
yaml
linter:
rules:
empty_catches: true
除非另有说明,否则本网站上的文档反映的是 Dart 3.7.1 版本。页面上次更新于 2025-03-07。 查看源代码 或 报告问题。