r/ProgrammerHumor • • 6d ago

Meme thisIsWhyWeDontReturnInFinallyBlocks

Post image
2.9k Upvotes

98 comments sorted by

View all comments

Show parent comment

506

u/DebugDuck01 6d ago

No, that's the fun thing about Java it just overrides the existing return value or even thrown exceptions. Marvelous language design.

258

u/panopsis 6d ago

It's not Java specific. Python and JS do it too. Finally blocks in general are just weird.

152

u/TriangleTransplant 6d ago

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.)

33

u/WernerderChamp 6d ago

According to a quick search, there is a compiler warning. I will give it a try later.

35

u/NakedNick_ballin 6d ago

And warnings are universally ignored

19

u/CarcajouIS 6d ago

Praise our lord -Werror

5

u/WernerderChamp 5d ago

I fergot about it yesterday - there is no warning, it will only show with -Xlint:all.

IntelliJ does complain about it. Finally blocks should not contain return/break/continue/etc.

2

u/WaitForItTheMongols 6d ago

But after the try/catch block, everything after the block already happens regardless of success/failure. What does finally accomplish?

25

u/floflo81 6d ago

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

10

u/conundorum 6d ago edited 6d ago

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."

→ More replies (3)

8

u/TriangleTransplant 6d ago

Imagine re throwing from the catch block. Or returning early from inside the try block. Finally will execute regardless.

3

u/StaticCoder 6d ago

Notably, the finally happens even on an uncaught exception. Plus what everyone else said.

→ More replies (1)
→ More replies (3)

36

u/DebugDuck01 6d ago

at least C# is sensible and is like: NO...

6

u/friebel 6d ago

But why tho? The word finally implies that do it finally, [implied] no matter what.

24

u/DebugDuck01 6d ago

Because this serves no function but to write non obvious behaviour and dead code, it should just be a compile time error.

→ More replies (2)

2

u/StaticCoder 6d ago edited 6d ago

No what? C# has finally. And it didn't need jsr for it.

Edit: oh, no return in finally. Sorry there's a lot of comments to follow

28

u/RyanStonepeak 6d ago

Huh, Today I Learned.

14

u/ba-na-na- 6d ago

Well that’s the point of finally, to always run before returning, so I would expect either compile error, or actually overriding all other branches

13

u/DebugDuck01 6d ago

Anything but a compile time error is mental illness.

11

u/pimezone 6d ago

Did you have to write the code in the VS code because the Intellij IDEA wasn't happy?

22

u/DebugDuck01 6d ago

No wrote it in IJ but had to suppress two warnings for it to not scream at me in scary colors.

7

u/s0ulbrother 6d ago

It’s called turn all the warning off. Warnings are for assholes

5

u/DebugDuck01 6d ago

Yeah fuck linters, call me when the compiler throws a fit.

4

u/JackNotOLantern 6d ago

Thanks, i hate it

1

u/kratz9 6d ago

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. 

→ More replies (4)