跳至主要內容

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 map 語法來設定 linter 規則,請在 linter > rules 下方新增 empty_catches: true

analysis_options.yaml
yaml
linter:
  rules:
    empty_catches: true