try... catch.. finally, which value gets returned?

I've been working quite a lot in Java. Although this is part of the university course, we actually have some QAs we need to satisfy, and so the quality of the code matters. Most modern lanauges are Turing Complete, so it is rarely about what you can do, but rather how you can do it. Every now and again (more often than not actually), I came across painfully annoying Java constructs which make the code that much harder to read and write. Like today, I placed my return statement in finally block (completely by accident). My code had a similar shape*:

* I feel the need to explain, that this was a dirty solution to check if my tests were working, the final solution got rid of the return in the try and only had the return at the end of the method

Lo and behold, I was getting 0 all the time. At first, I didn't really notice the fact that this is where I placed in the finally block. Even when I discovered it was few moments before I made the connection (inference that the value was 0 all the time). Just for fun I checked and in C# you get a compiler error as change of flow is not allowed in the finally.

So yeah... The Java way.

Leave Comment

Your email address will not be published.

Please type the characters of this captcha image in the input box

Please type the characters of this captcha image in the input box