Finally always executes. That's what it's for, which means its return value gets used in your case.
You'll want to change your code so it's more like this:
function example() {
var returnState = false; // initialization value is really up to the design
try {
returnState = true;
}
catch {
returnState = false;
}
finally {
return returnState;
}
}
Generally speaking you never want to have more than one return statement in a function, things like this are why.
Answer from annakata on Stack OverflowFinally always executes. That's what it's for, which means its return value gets used in your case.
You'll want to change your code so it's more like this:
function example() {
var returnState = false; // initialization value is really up to the design
try {
returnState = true;
}
catch {
returnState = false;
}
finally {
return returnState;
}
}
Generally speaking you never want to have more than one return statement in a function, things like this are why.
According to ECMA-262 (5ed, December 2009), in pp. 96:
The production
TryStatement : try Block Finallyis evaluated as follows:
- Let B be the result of evaluating Block.
- Let F be the result of evaluating Finally.
- If F.type is normal, return B.
- Return F.
And from pp. 36:
The Completion type is used to explain the behaviour of statements (
break,continue,returnandthrow) that perform nonlocal transfers of control. Values of the Completion type are triples of the form (type, value, target), where type is one ofnormal,break,continue,return, orthrow, value is any ECMAScript language value or empty, and target is any ECMAScript identifier or empty.
It's clear that return false would set completion type of finally as return, which cause try ... finally to do 4. Return F.
Videos
Finally always executes. That's what it's for, which means its return value gets used in your case.
You'll want to change your code so it's more like this:
function example() {
var returnState = false; // initialization value is really up to the design
try {
returnState = true;
}
catch {
returnState = false;
}
finally {
return returnState;
}
}
Generally speaking you never want to have more than one return statement in a function, things like this are why.
According to ECMA-262 (5ed, December 2009), in pp. 96:
The production
TryStatement : try Block Finallyis evaluated as follows:
- Let B be the result of evaluating Block.
- Let F be the result of evaluating Finally.
- If F.type is normal, return B.
- Return F.
And from pp. 36:
The Completion type is used to explain the behaviour of statements (
break,continue,returnandthrow) that perform nonlocal transfers of control. Values of the Completion type are triples of the form (type, value, target), where type is one ofnormal,break,continue,return, orthrow, value is any ECMAScript language value or empty, and target is any ECMAScript identifier or empty.
It's clear that return false would set completion type of finally as return, which cause try ... finally to do 4. Return F.
So I learned this the hard way today - does anyone have any documentation for this behavior? It seems weird to me. Even with a return statement, the finally block will run. I tested this in our Flutter mobile app as well as Dart Pad just now.
void main() {
print(doThing());
// prints "still running finaly block" then 42
}
doThing() {
try {
return aux();
} finally {
print('still running finally block');
}
}
int aux() => 42;Of course, code shouldn't be doing this when you think about it, because that's what finally blocks are there for, but it came about from a refactor and I thought it was interesting.
EDIT: Thanks for the responses; turns out I was not versed in the interaction of a finally blocks and return statements, because this is the way it works in many languages.
finally basically runs even if you have an early-return from try-catch or even if you don't handle the error in the try-catch. Here is an example I like:
function myFunction() {
try {
console.log('inside "try"');
return
} finally {
console.log('inside "finally"');
}
console.log("after try-finally");
}
myFunction()
When you run myFunction(), it will print the following:
inside "try"
inside "finally"
Since you returned from try, it didn't execute any instructions after the try-finally block. But, Javascript did execute the finally block.
Thies are example codes in MDN web docs to explain the important of finally block and it behaviour.
openMyFile();
try {
// tie up a resource
writeMyFile(theData);
} finally {
closeMyFile(); // always close the resource
}
Accordig to the MDN documentation finally execute no matter what the logic fail or not inside the try block.
In most cases inside a function there are code blocks must execute before workflow end.
Ex: no matter what if file is get opened it must close before exit from workflow.
function doIt() {
try {
return 1;
} finally {
return 2;
}
}
doIt(); // returns 2
In above code whether it should return 1. It will return 2 because it guarantee the execution of code inside finally block.But according to MDN this is a bad implementation.
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch