It serves to provide a space to clean up after the code in the main block, for example close IO streams, etc. It is absolutely necessary for certain applications, though Java's "try with resources" does cater for the major use case for which it was invented in, IMO, a much cleaner way.
If C# isn't making a point to do finally, then either they found a magical way to make sure resources allocated in the main block get closed, or they screwed the implementation up. That said, Microsoft says they do work the same way as Java's on their Microsoft Learn page "Exception-handling statements - throw and try, catch, finally - C# references":
In a try-finally statement, the finally block runs when control leaves the try block. Control might leave the try block as a result of
normal execution,
execution of a jump statement (that is, return, break, continue, or goto), or
propagation of an exception out of the try block.
I didn't know it, but knowing it now actually makes me understand why finally exists in the first place. It didn't seem that hard to put something after the try {} catch that does the same thing! It executing in the event of an unhandled exception or return makes perfect sense.
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.)
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
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."
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
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.
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?
187
u/BlackFrank98 5h ago
Doesn't this always return CEO.current() or CEO.findNew() without ever getting to CEO.jensenHuang?
48
u/pimezone 5h ago
There should be the line in
finally6
u/dcondor07uk 5h ago
Where is the findNew function defined?
13
u/warmowed 2h ago
An actual programming joke! In this economy!?!
8
u/averagecrazyliberal 3h ago
This is exactly what I want to see in this sub. No more is_even() please for the love of god.
13
u/guywithbeard 5h ago
NullPointerException: HonypotException not found
2
u/Decoder44 3h ago
Honeypot spelling is wrong 😭
4
u/purbub 5h ago
Static method and properties? Daring today aren’t we?
10
u/CaNaDaYaN 5h ago
This should just not even compile, stupid java.
1
u/sandybuttcheekss 3h ago
Does find new just return Jensen Huang and have a to-do to update it later?
1
u/Arian-ki 1h ago
Jensen should be an instance of CEO tho
0
u/Symphonic_nerve 5h ago
🤣🤣🤣🤣
-7
u/MapNo2659 5h 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?
1
u/JacksOnF1re 1h ago
Honypot, hornypot. Whatever.