Skip to content

noUnsafeFinally

诊断类别:lint/correctness/noUnsafeFinally

¥Diagnostic Category: lint/correctness/noUnsafeFinally

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

禁止在 finally 块中控制流语句。

¥Disallow control flow statements in finally blocks.

JavaScript 暂停 trycatch 块的控制流语句,直到 finally 块执行完成。所以,当在 finally 中使用 returnthrowbreakcontinue 时,trycatch 内的控制流语句将被覆盖,这被视为意外行为。

¥JavaScript suspends the control flow statements of try and catch blocks until the execution of finally block finishes. So, when return, throw, break or continue is used in finally, control flow statements inside try and catch are overwritten, which is considered as unexpected behavior.

¥Examples

¥Invalid

(() => {
try {
return 1; // 1 is returned but suspended until finally block ends
} catch(err) {
return 2;
} finally {
return 3; // 3 is returned before 1, which we did not expect
}
})();
code-block.js:7:9 lint/correctness/noUnsafeFinally ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unsafe usage of ‘return’.

5 │ return 2;
6 │ } finally {
> 7 │ return 3; // 3 is returned before 1, which we did not expect
^^^^^^^^^
8 │ }
9 │ })();

‘return’ in ‘finally’ overwrites the control flow statements inside ‘try’ and ‘catch’.

(() => {
try {
throw new Error("Try"); // error is thrown but suspended until finally block ends
} finally {
return 3; // 3 is returned before the error is thrown, which we did not expect
}
})();
code-block.js:5:9 lint/correctness/noUnsafeFinally ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unsafe usage of ‘return’.

3 │ throw new Error(“Try”); // error is thrown but suspended until finally block ends
4 │ } finally {
> 5 │ return 3; // 3 is returned before the error is thrown, which we did not expect
^^^^^^^^^
6 │ }
7 │ })();

‘return’ in ‘finally’ overwrites the control flow statements inside ‘try’ and ‘catch’.

(() => {
try {
throw new Error("Try")
} catch(err) {
throw err; // The error thrown from try block is caught and re-thrown
} finally {
throw new Error("Finally"); // Finally(...) is thrown, which we did not expect
}
})();
code-block.js:7:9 lint/correctness/noUnsafeFinally ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unsafe usage of ‘throw’.

5 │ throw err; // The error thrown from try block is caught and re-thrown
6 │ } finally {
> 7 │ throw new Error(“Finally”); // Finally(…) is thrown, which we did not expect
^^^^^^^^^^^^^^^^^^^^^^^^^^^
8 │ }
9 │ })();

‘throw’ in ‘finally’ overwrites the control flow statements inside ‘try’ and ‘catch’.

(() => {
label: try {
return 0; // 0 is returned but suspended until finally block ends
} finally {
break label; // It breaks out the try-finally block, before 0 is returned.
}
return 1;
})();
code-block.js:5:7 lint/correctness/noUnsafeFinally ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unsafe usage of ‘break’.

3 │ return 0; // 0 is returned but suspended until finally block ends
4 │ } finally {
> 5 │ break label; // It breaks out the try-finally block, before 0 is returned.
^^^^^^^^^^^^
6 │ }
7 │ return 1;

‘break’ in ‘finally’ overwrites the control flow statements inside ‘try’ and ‘catch’.

function a() {
switch (condition) {
case 'a': {
try {
console.log('a');
return;
} finally {
break;
}
}
case 'b': {
console.log('b');
}
}
}
code-block.js:8:9 lint/correctness/noUnsafeFinally ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unsafe usage of ‘break’.

6 │ return;
7 │ } finally {
> 8 │ break;
^^^^^^
9 │ }
10 │ }

‘break’ in ‘finally’ overwrites the control flow statements inside ‘try’ and ‘catch’.

¥Valid

let foo = function() {
try {
return 1;
} catch(err) {
return 2;
} finally {
console.log("hola!");
}
};
let foo = function() {
try {
return 1;
} catch(err) {
return 2;
} finally {
let a = function() {
return "hola!";
}
}
};
let foo = function(a) {
try {
return 1;
} catch(err) {
return 2;
} finally {
switch(a) {
case 1: {
console.log("hola!")
break;
}
}
}
};

¥Related links