跳到主要內容

use_rethrow_when_possible

穩定
建議
可修正

使用 rethrow 重新拋出捕獲的例外。

詳細資訊

#

出自 Effective Dart

務必 使用 rethrow 重新拋出捕獲的例外。

由於 Dart 提供 rethrow 作為一項功能,應加以使用以提升簡潔度和可讀性。

不良示範

dart
try {
  somethingRisky();
} catch(e) {
  if (!canHandle(e)) throw e;
  handle(e);
}

良好示範

dart
try {
  somethingRisky();
} catch(e) {
  if (!canHandle(e)) rethrow;
  handle(e);
}

啟用

#

若要啟用 use_rethrow_when_possible 規則,請在您的 analysis_options.yaml 檔案中的 linter > rules 下方新增 use_rethrow_when_possible

analysis_options.yaml
yaml
linter:
  rules:
    - use_rethrow_when_possible

如果您改為使用 YAML 對應語法來設定 linter 規則,請在 linter > rules 下方新增 use_rethrow_when_possible: true

analysis_options.yaml
yaml
linter:
  rules:
    use_rethrow_when_possible: true