跳到主要内容

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