views:

87

answers:

6

How a return statement inside a try/catch block works?

function example() {
    try {
        return true;
    }
    finally {
        return false;
    }
}

I'm expecting the output of this function to be "true", but instead is "false"!

+3  A: 

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.)

GenericTypeTea
A: 

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.

Manoj Govindan
+1  A: 

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;
    }
}
anishmarokey
A: 

This is an interesting question. Finally is supposed to ALWAYS run at the end of a try catch block so that may be why you are getting false returned. Which browsers have you tried? It is entirely possible that different browsers have different implementations

Darko Z
IE8, Firefox 3.6 and Chrome 6: all the same ;)
bonfo
+8  A: 

Finally always executes. That's what it's for, which means it's return gets used in your case.

You'll want to change your code so it's more like this:

function example() { 
    var returnState = false; // initialisation 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.

annakata
I would argue having more than one return statement isn't always bad - See http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement for more discussion.
Castrohenge
+1 good answer. disagree on one return in a function though.
Darko Z
I too disagree about the one return rule. You should never return from finally, though (in C#, it's not even allowed).
erikkallen
@erikkallen - that's a good point. A return outside the T-C-F block would be best but the example code would be a bit forced :)
annakata
@Castrohenge - it's not a hard and fast rule, but most of the copunter-examples in that thread are pretty contrived, and the only valid case I see is the "guard clause" (essentially the pattern of checking input data at the top of the function and returning conditionally). That's a perfectly valid case, but really those returns should be exceptions (again not hard and fast).
annakata
+1  A: 

According to ECMA-262 (5ed, December 2009), in pp. 96:

The production TryStatement : try Block Finally is evaluated as follows:
1. Let B be the result of evaluating Block.
2. Let F be the result of evaluating Finally.
3. If F.type is normal, return B.
4. Return F.

And from pp. 36:

The Completion type is used to explain the behaviour of statements (break, continue, return and throw)
that perform nonlocal transfers of control. Values of the Completion type are triples of the form (type, value,
target), where type is one of normal, break, continue, return, or throw, 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.

livibetter