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 preset.

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 processData() {
try {
return fetchData();
} finally {
return null;
}
}
function handleError() {
try {
throw new Error("Original error");
} catch (error) {
throw error;
} finally {
throw new Error("Override error");
}
}
for (let i = 0; i < items.length; i++) {
try {
processItem(items[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 ❤️‍🔥 in Boston by Josh Goldberg and contributors.