目錄

use_rethrow_when_possible

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

此規則自 Dart 2.0 起可用。

規則集:建議flutter

此規則有可用的快速修正

詳細資訊

#

來自有效的 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