メインコンテンツにスキップ

use_rethrow_when_possible

安定版
推奨
修正が利用可能です

キャッチした例外を再スローするにはrethrowを使用します。

詳細

#

Effective Dart」より

DO キャッチした例外を再スローするには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 > rulesの下にuse_rethrow_when_possible: trueを追加します。

analysis_options.yaml
yaml
linter:
  rules:
    use_rethrow_when_possible: true