跳到主要内容

empty_catches

稳定
核心
有可用修复

避免空的 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