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

throw_in_finally

安定版

finally ブロックでの throw は避けてください。

詳細

#

finally ブロックで例外をスローすることは避けてください

finally ブロックで例外をスローすると、デバッグが困難な予期しない動作が必然的に発生します。

悪い例

dart
class BadThrow {
  double nonCompliantMethod() {
    try {
      print('hello world! ${1 / 0}');
    } catch (e) {
      print(e);
    } finally {
      throw 'Find the hidden error :P'; // LINT
    }
  }
}

良い例

dart
class Ok {
  double compliantMethod() {
    var i = 5;
    try {
      i = 1 / 0;
    } catch (e) {
      print(e); // OK
    }
    return i;
  }
}

有効にする

#

throw_in_finally ルールを有効にするには、analysis_options.yaml ファイルの linter > rules の下に throw_in_finally を追加してください。

analysis_options.yaml
yaml
linter:
  rules:
    - throw_in_finally

代わりに YAML マップ構文を使用してリンタールールを設定している場合は、linter > rules の下に throw_in_finally: true を追加してください。

analysis_options.yaml
yaml
linter:
  rules:
    throw_in_finally: true