58 lines
1.7 KiB
Plaintext
58 lines
1.7 KiB
Plaintext
The purpose of a `finally` block is to ensure the cleanup of objects (e.g. closing the file), it is executed after `try` and `catch` blocks under any circumstances. Having a `return` or a thrown exception in a ``++finally++`` block suppresses the propagation of any unhandled exception which was thrown in the ``++try++`` or ``++catch++`` block or overwrite the returned value.
|
|
|
|
This rule will report on all usages of jump statements (``++return++``, ``++break++``, ``++throw++``, and ``++continue++``) 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` to have the logic there limited to the "cleanup".
|
|
|
|
== Noncompliant Code Example
|
|
|
|
[source,javascript]
|
|
----
|
|
async function foo() {
|
|
let result, connection;
|
|
try {
|
|
connection = await connect();
|
|
result = connection.send(1);
|
|
} catch(err) {
|
|
console.error(err.message); // if this line throws, this exception will not be propagated
|
|
} finally {
|
|
connection.close();
|
|
return result; // Noncompliant
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
[source,javascript]
|
|
----
|
|
async function foo() {
|
|
let result, connection;
|
|
try {
|
|
connection = await connect();
|
|
result = connection.send(1);
|
|
} catch(err) {
|
|
console.error(err.message);
|
|
} finally {
|
|
connection.close();
|
|
}
|
|
return result;
|
|
}
|
|
----
|
|
|
|
include::../see.adoc[]
|
|
|
|
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[]
|