Which returns false. I guess this is because of the guarantee that the finally block will be run, no matter what. The JS interpreter must be catching the return and holding it pending the execution of the finally block.
Checking with
try { return true; } finally { alert('hi'); }
Shows this to be true. (It both returns true AND alerts 'hi'.)
I see it that the "finally" is run right before the return. So, the statement "true" would be evaluated, then control would go to the "finally" block, where the method would return false.
I agree; the trickiest thing about it is the confusing relationship between the syntax for exception handling and the syntax for cleanup blocks, which are only vaguely related concepts.
It appears MS thought this through. The contrived example is but ONE way a mediocre programmer could really munge this stuff up if the language is not specific about protecting exit points from a finall{} block.
What about throwing exceptions in the finally block? Isn't it an exit point too?
When you call non-trivial 3rd party code from your finally block, how do you ensure it doesn't throw? Or what do you do if it does? Indefinitely nested try blocks?
I know it's horrible in practice, and I really can't see a valid use for this particular case, but forbidding things like this (or other things like multiple inheritance) just because it can be abused has never seemed like a good idea to me.
It's not just that it can be abused, though - every use of it is an abuse. Finally{} is not for decisions and manipulating values and program logic, it's for cleaning up resources.
2.6.2 as well. I liked this little snippet, though I grumbled a little on the inside and would be appalled if I ever found something like that in serious code...
Its a tricky question, but not something I would use to find a "keeper". From the Java tutorial - "The finally block _always_ executes when the try block exits." After the interpreter executes the "return true;" statement, it exits the try block. And then, if you know that "The finally block _always_ executes when the try block exits.", you know that the code block will return false;
I wonder about the merits of this sort of thing as an interview screen, though. There's not necessarily a strong correlation between knowledge of language esoterica and other more important qualities like perseverance and raw intelligence.
The point is not to assess language knowledge, but disposition. Even if the candidate is unamused, they should still instantly get what the interviewer found punny about it.
The test is not whether you know the correct answer, but whether your brow furrows when you read the code. If you don't find the snippet interesting you fail.
The ultimate screen would be to follow the candidate home and see if they Google or test the answer.
33 comments
[ 5.5 ms ] story [ 84.4 ms ] threadIt's not really a "paradox" so much as it is very bad style to "return" from a "finally".
Edit
My partner Dave has a much better trick question:
"You win", I told him. "Nah, Google won. But nobody really wins if you write code like that."I realize that's not the point, but I figured I'd mention it anyway.
try { return true; } finally { return false; }
Which returns false. I guess this is because of the guarantee that the finally block will be run, no matter what. The JS interpreter must be catching the return and holding it pending the execution of the finally block.
Checking with
try { return true; } finally { alert('hi'); }
Shows this to be true. (It both returns true AND alerts 'hi'.)
When you call non-trivial 3rd party code from your finally block, how do you ensure it doesn't throw? Or what do you do if it does? Indefinitely nested try blocks?
try: return True finally: return False
-- There's an error in your program: * 'return' outside function ...
Just sayin'
The ultimate screen would be to follow the candidate home and see if they Google or test the answer.