Yes, the finally block will be executed even after a return statement in a method. The finally block will always execute even an exception occurred or not in Java.
Yes, we can write a return statement of the method in catch and finally block. There is a situation where a method will have a return type and we can return some value at any part of the method based on the conditions.
Upon the completion of finally block execution, control goes back to the return statement in the try block and returns “returning from try block”. If finally block has a return statement, then the return statements from try/catch blocks will be overridden.
If the return in the try block is reached, it transfers control to the finally block, and the function eventually returns normally (not a throw).
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 Finally
is 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
,return
andthrow
) 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.
When you use finally
, any code within that block fires before the method exits. Because you're using a return in the finally
block, it calls return false
and overrides the previous return true
in the try
block.
(Terminology might not be quite right.)
The finally block rewrites try block return (figuratively speaking).
Just wanted to point out, that if you return something from finally, then it will be returned from the function. But if in finally there is no 'return' word - it will be returned the value from try block;
function example() {
try {
return true;
}
finally {
console.log('finally')
}
}
console.log(example());
// -> finally
// -> true
So -finally- return
rewrites the return of -try- return
.
why you are getting false is you returned in a finally block. finally block should execute always. so your return true
changes to return false
function example() {
try {
return true;
}
catch {
return false;
}
}
I'm gonna give a slightly different answer here: Yes, both the try
and finally
block get executed, and finally
takes precedence over the actual "return" value for a function. However, these return values aren't always used in your code.
Here's why:
res.send()
from Express.js, which creates a HTTP response and dispatches it.try
and finally
block will both execute this function like so:try {
// Get DB records etc.
return res.send('try');
} catch(e) {
// log errors
} finally {
return res.send('finally');
}
This code will show the string try
in your browser. ALSO, the example will show an error in your console. The res.send()
function is called twice. This will happen with anything that is a function. The try-catch-finally block will obfuscate this fact to the untrained eye, because (personally) I only associate return
values with function-scopes.
Imho your best bet is to never use return
inside a finally
block. It will overcomplicate your code and potentially mask errors.
In fact, there's a default code inspection rule set-up in PHPStorm that gives a "Warning" for this:
https://www.jetbrains.com/help/phpstorm/javascript-and-typescript-return-inside-finally-block.html
finally
for?I would use finally
only to clean-up stuff. Anything that is not critical for the return value of a function.
It may make sense if you think about it, because when you depend on a line of code under finally
, you are assuming that there could be errors in try
or catch
. But those last 2 are the actual building blocks of error handling. Just use a return
in try
and catch
instead.
As far as I know, the finally
block always executes, irrespective of whether you have a return
statement inside try
or not. Ergo, you get the value returned by the return
statement inside finally block.
I tested this with Firefox 3.6.10 and Chrome 6.0.472.63 both in Ubuntu. It is possible that this code may behave differently in other browsers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With