- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Edit Admin
Today I learnt that you can actually a return statement in the finally branch in Java (I guess). What a weird language.
Addendum 2025-02-12 06:47: BTW someone here more into Java these days around and can me explain what is going on?
Is there always returned null no matter what in this case?
Is finally in this case only executed when an exception happens?
Or is there some magic condition happening and it doesn't execute the return code but the original one in case there is not exception? How would that even work if the code is more complex though?
Admin
And considering that the finally branch is always executed, what will be returned when creating the connection succeeds? Will it return the connection or will it return null?
Admin
The real problem is that the finally block will ALWAYS return null, no matter what. It doesn't matter if the connection fails or succeed because the method will only give you a piece of the void living on the author of this code's head.
Admin
A return or throw in a finally block will discard anything that's returned or thrown from a try or catch block. In other words, this code will always return null, and nothing else.
Admin
You're missing the essence here for what a return statement in a finally block does in Java. It will actually override the previous return value, so even if it doesn't fail while creating the connection, it will still return null.
The only reason this could have been successfully tested (I'm not going to speculate on what the success criteria were for those test) is if the testers actually followed the instruction to uncomment the other return statement in the finally block.
Admin
I was wondering what would happen in C# (as I suspected that finally's return would overwrite the previous one), so I tried a return in both the
try
and thefinally
. However the compiler wouldn't let me: "CS0157 Control cannot leave the body of a finally clause". It sort of confirms my opinion of JavaEdit Admin
TRWTF is Java. I usually try not to poo-poo on it (me mainly being a C# dev), but this is just so wrong. Like, people who maintain Java should accept a hard compilation error break across the ecosystem and make returns inside finally blocks illegal. It's that bad.
Edit Admin
I worked at a place (not my dept) where it was discovered that for year anyone with certain punctuation in their name always failed on the checkout page with an exception, which was logged each time - but no one ever looked at the logs. So for the lack of looking at logs, lots of revenue was lost.
Edit Admin
Presumably that means that a finally block mustn't leak exceptions either.
Edit Admin
Of course there are umpteen thousands of trolls all over the Internet posting fake stories, but I have no trouble at all believing this. I've not only seen plenty of development shops, I've seen plenty of production environments and the people who staff them. Admins noticing the problem would be the exception, not the rule.