empty_catches
避免使用空的 catch 區塊。
此規則自 Dart 2.0 起提供。
此規則有可用的快速修復。
詳細資訊
#避免空的 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
除非另有說明,否則本網站上的文件反映 Dart 3.6.0。頁面最後更新於 2024-07-03。 檢視原始碼或 回報問題。