control_flow_in_finally
Avoid control flow in finally
blocks.
Details
#AVOID control flow leaving finally
blocks.
Using control flow in finally
blocks will inevitably cause unexpected behavior
that is hard to debug.
BAD:
dart
class BadReturn {
double nonCompliantMethod() {
try {
return 1 / 0;
} catch (e) {
print(e);
} finally {
return 1.0; // LINT
}
}
}
BAD:
dart
class BadContinue {
double nonCompliantMethod() {
for (var o in [1, 2]) {
try {
print(o / 0);
} catch (e) {
print(e);
} finally {
continue; // LINT
}
}
return 1.0;
}
}
BAD:
dart
class BadBreak {
double nonCompliantMethod() {
for (var o in [1, 2]) {
try {
print(o / 0);
} catch (e) {
print(e);
} finally {
break; // LINT
}
}
return 1.0;
}
}
GOOD:
dart
class Ok {
double compliantMethod() {
var i = 5;
try {
i = 1 / 0;
} catch (e) {
print(e); // OK
}
return i;
}
}
Enable
#To enable the control_flow_in_finally
rule,
add control_flow_in_finally
under linter > rules in your
analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- control_flow_in_finally
If you're instead using the YAML map syntax to configure linter rules,
add control_flow_in_finally: true
under linter > rules:
analysis_options.yaml
yaml
linter:
rules:
control_flow_in_finally: true
除非另有说明,文档之所提及适用于 Dart 3.7.2 版本,本页面最后更新时间: 2025-03-07。 查看文档源码 或者 报告页面问题。