== Why is this an issue?

The `finally` block is a part of a `try...catch...finally` statement, which allows you to handle errors and perform cleanup operations regardless of whether an exception is thrown or not. The `finally` block is executed regardless of whether an exception occurs or not, and it is placed after the `try` and `catch` blocks.

Having a jump statement, such as `return`, `break`, `continue`, or `throw`, inside a `finally` block can lead to unexpected and undesirable behavior, making your code difficult to understand and maintain. While it's not inherently forbidden to use jump statements in `finally` blocks, it is generally discouraged for the following reasons:

* The primary purpose of the `finally` block is to ensure cleanup operations and code that must run regardless of the outcome, such as releasing resources or closing connections. If you use a `return` statement inside the finally block, it will override any previous `return` statements in the `try` or `catch` blocks. This can lead to unexpected values being returned from a function.
* Jump statements like `break`, `continue`, or even another `throw` inside the `finally` block can alter the normal control flow of the program. This can make it difficult to reason about the behavior of the code and may introduce subtle bugs that are hard to detect.
* If a `return` or `throw` statement inside the `finally` block causes a new exception or alters the return value, it can hide or suppress the original exception or return value from the `try` or `catch` blocks. This can make it challenging to identify the actual cause of an error.
* Code that uses jump statements in `finally` blocks can be hard to read and understand, especially for other developers who might not be familiar with the unusual control flow. Such code can lead to maintenance issues and make it harder to debug and maintain the application in the long run.

This rule reports on all usages of jump statements from a ``++finally++`` block. Even if it's guaranteed that no unhandled exception can happen in ``++try++`` or ``++catch++`` blocks, it's not recommended to use any jump statements inside the `finally` block to have the logic there limited to the "cleanup".

[source,javascript,diff-id=1,diff-type=noncompliant]
----
async function foo() {
    let result, connection;
    try {
        connection = await connect();
        result = connection.send(1);
    } catch(err) {
        console.error(err.message);
    } finally {
        if (connection) {
            connection.close();
        }
        return result; // Noncompliant: Jump statement 'return' in the 'finally' block
    }
}
----

While there might be rare cases where using jump statements in a `finally` block is necessary, it's generally recommended to avoid it whenever possible. Instead, use the `finally` block only for cleanup operations and critical tasks that should always be executed, regardless of exceptions or return values.

[source,javascript,diff-id=1,diff-type=compliant]
----
async function foo() {
    let result, connection;
    try {
        connection = await connect();
        result = connection.send(1);
    } catch(err) {
        console.error(err.message);
    } finally {
        if (connection) {
            connection.close();
        }
    }
    return result;
}
----

== Resources
=== Documentation

* MDN web docs - link:++https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch#the_finally_block++[The ``++finally++`` block]
* MDN web docs - link:++https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch++[``++try...catch++``]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return[``++return++``]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw[``++throw++``]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break[``++break++``]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue[``++continue++``]

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

include::../message.adoc[]

'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
