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 规则,请在 linter > rules 下添加 throw_in_finally: true。
analysis_options.yaml
yaml
linter:
rules:
throw_in_finally: true