目次

throw_in_finally

finallyブロック内でthrowを使用しないでください。

このルールはDart 2.0から利用可能です。

詳細

#

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