Of course a finally block is useful it's returning a value from it that is stupid. C# has finally blocks it just throws a compile error if you pull the highly regarded move of using return within.
The use case for them is valid: doing cleanup that needs to happen regardless of success or failure (Go uses defer for the same purpose, for example). But it feels like it should be a compilation error to return from a finally block (or at least a lint error/warning.)
You know how C has the single-return idiom, where every function that needs cleanup will defer to a cleanup section at the end of the function before it actually returns? try ... finally is a language construct for enforcing that.
It also doubles as a replacement for C++ RAII, because Java was designed before people really understood how useful doing cleanup in a constructor destructor actually is.
Wow, that's a silly typo. Derp.
Now, imagine a C single-return cleanup block, or C++ destructor, that just turned into Adam Savage and said, "I reject your return statement and substitute my own."
I'm not sure about RAII being well understood when Java was made, but I was pretty clear that C++ is older than Java. They couldn't design the feature as a replacement for RAII if RAII didn't exist, after all.
I started using C++ roughly the year Java was released, and I'm pretty sure it already had destructors that could close files in its library. I find it hard to believe that "RAII didn't exist" in the 6 years between when C++ was released and Java was designed.
The "finally" block will be executed even if an unhandled exception is thrown or if a "return" instruction is executed in the "try" block or the "catch" block. That's the point
Huh, I've never thought to try this (since it makes no sense) Now Im curious what C# or VB. Net do... and neither allow it. Supposedly a compile time error.
Finally in java is a block that will always run, regardless of what happens within the try/catch blocks
It's meant for closing OS resources and things like that, and is the only case where you can run code after a return instruction
If you put a return inside a finally block, it will always run, meaning it will override the returns inside the try/catch blocks
In the pictured code, if an exception is thrown, CEO.findNew() will run and will apply side effects if there are any, but the function will still always return CEO.jensenHuang
It's a really weird quirk of java, and i have no clue why they don't simply throw a compiler error when you try to return on a finally block to prevent this very unexpected behavior
return 0 does stop the execution of the try block and attempts to return 0, but finally will always execute no matter what, and it will print "c" and override the return to 1
Returning from the finally block will truly end execution though, so print("d") and return 2 are unreachable code
No one of the points of the finally block is precisely that it happens no matter what, even if you return. Now if you have nested finallys and they both return, I have no idea which one wins.
Presumably the outer finally. Finally blocks are only guaranteed to run if execution enters the associated try block, so the only case this could apply is nested try blocks.
In 10 years I never saw a return in a finally outside of knowledge exams. It's one of the few things I would consider an immediate refusal on the code review.
Basic research tells you that finally is guaranteed to run before the function actually returns. That should be enough to tell you that a return statement in the finally block is guaranteed to run, too.
That's like saying that "if being a branch statement is absolutely not something you should notice with basic research".
Wouldn't your research tell you that the previous return also defines the return value of a function? It's the unstoppable force vs the immovable object.
I would be very surprised if any Java dev could tell with 100% certainty the return value (or exception) of that function without trying it out themselves.
You can do some package manager manipulation to specify the class as provided at runtime, which will then make it a runtime exception. It wouldn't be a NullPointerException, it would be a ClassNotFoundException.
Haha, this reminds me of the time I accidentally put a return statement in a finally block during a late-night coding session. Debugging that was a nightmare! Anyone else have a similar horror story?
340
u/BlackFrank98 3d ago
Doesn't this always return CEO.current() or CEO.findNew() without ever getting to CEO.jensenHuang?
71
u/pimezone 3d ago
There should be the line in
finally9
u/dcondor07uk 3d ago
Where is the findNew function defined?
8
u/Decoder44 3d ago
Honeypot spelling is wrong 😭
66
u/warmowed 3d ago
An actual programming joke! In this economy!?!
33
u/averagecrazyliberal 3d ago
This is exactly what I want to see in this sub. No more is_even() please for the love of god.
17
u/guywithbeard 3d ago
NullPointerException: HonypotException not found
4
u/purbub 3d ago
Static method and properties? Daring today aren’t we?
12
u/CaNaDaYaN 3d ago
This should just not even compile, stupid java.
2
u/sandybuttcheekss 3d ago
Does find new just return Jensen Huang and have a to-do to update it later?
1
u/Arian-ki 3d ago
Jensen should be an instance of CEO tho
1
u/2sACouple3sAMurder 3d ago
Hony 2012
1
u/vixfew 3d ago
Oh wow, programming joke with code with correct syntax and logic. Outstanding
1
u/TopFix8731 3d ago
Dont use erro handling for extra matrimonial business rules dude
1
u/realrealvantheman 3d ago
Ive never worked with object oriented programming what does this even do
1
u/ivanrj7j 2d ago
i just want to know why are you using static variables and methods everywhere?
0
u/Symphonic_nerve 3d ago
🤣🤣🤣🤣
-10
u/MapNo2659 3d ago
Haha, this reminds me of the time I accidentally put a return statement in a finally block during a late-night coding session. Debugging that was a nightmare! Anyone else have a similar horror story?
2
u/JacksOnF1re 3d ago
Honypot, hornypot. Whatever.