Skip to content

finallyStatementSafety

Reports control flow statements in finally blocks that can override control flow in try/catch blocks.

✅ This rule is included in the ts logical and logicalStrict presets.

Control flow statements such as return, throw, break, and continue in finally blocks can cause unexpected behavior. When these statements appear in a finally block, they override any control flow statements in the corresponding try or catch blocks. The finally block always executes, and its control flow statements take precedence, which can lead to bugs that are difficult to diagnose.

function
function processData(): any
processData
() {
try {
return
const fetchData: any
fetchData
();
} finally {
return null;
}
}
function
function handleError(): void
handleError
() {
try {
throw new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
("Original error");
} catch (
function (local var) error: unknown
error
) {
throw
function (local var) error: unknown
error
;
} finally {
throw new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
("Override error");
}
}
for (let
let i: number
i
= 0;
let i: number
i
<
const items: any
items
.
any
length
;
let i: number
i
++) {
try {
const processItem: any
processItem
(
const items: any
items
[
let i: number
i
]);
} finally {
break;
}
}

This rule is not configurable.

If you are very confident in your handling of exception handling in your code and specifically want to use the confusing quirks of finally control flow statements, this rule might not be for you.

Made with ❤️‍🔥 around the world by the Flint team and contributors.