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