throw_ in_ finally
Avoid throw
in finally
block.
Details
#AVOID throwing exceptions in finally
blocks.
Throwing exceptions in
finally
blocks will inevitably cause unexpected
behavior that is hard to debug.
BAD:
class BadThrow {
double nonCompliantMethod() {
try {
print('hello world! ${1 / 0}');
} catch (e) {
print(e);
} finally {
throw 'Find the hidden error :P'; // LINT
}
}
}
GOOD:
class Ok {
double compliantMethod() {
var i = 5;
try {
i = 1 / 0;
} catch (e) {
print(e); // OK
}
return i;
}
}
Enable
#
To enable the
throw_in_finally
rule, add
throw_in_finally
under
linter > rules
in your
analysis_options.yaml
file:
linter:
rules:
- throw_in_finally
If you're instead using the YAML map syntax to configure linter rules,
add
throw_in_finally: true
under
linter > rules:
linter:
rules:
throw_in_finally: true
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Report an issue.