throw_in_finally
Avoid throw
in finally
block.
This rule is available as of Dart 2.0.
Details
#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;
}
}
Usage
#To enable the throw_in_finally
rule,
add throw_in_finally
under linter > rules in your
analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- throw_in_finally
除非另有说明,文档之所提及适用于 Dart 3.5.4 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。