Strange finally behaviour? [duplicate]

That’s because you returned a value that was evaluated from q before you changed the value of q in the finally block. You returned q, which evaluated its value; then you changed q in the finally block, which didn’t affect the loaded value; then the return completed, using the evaluated value.

Don’t write tricky code like this. If it confuses the guy who wrote it, imagine the problems it will cause the next guy, a few years down the track when you are somewhere else.

Leave a Comment