目录

throw_in_finally

Avoid throw in finally block.

此规则自 Dart 2.0 版本起可用。

详情

#

AVOID throwing exceptions in finally blocks.

Throwing exceptions in finally blocks will inevitably cause unexpected behavior that is hard to debug.

BAD:

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

GOOD:

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