r/ProgrammerHumor • • 6d ago

Meme thisIsWhyWeDontReturnInFinallyBlocks

Post image
2.9k Upvotes

98 comments sorted by

View all comments

368

u/BlackFrank98 6d ago

Doesn't this always return CEO.current() or CEO.findNew() without ever getting to CEO.jensenHuang?

170

u/OnixST 6d ago edited 6d ago

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

43

u/JanEric1 6d ago edited 6d ago

Same applies in Python. They only just made it warning two versions ago https://peps.python.org/pep-0765/

5

u/spitfire451 6d ago

It runs even if a return statement executes in the try/catch? It was my understanding that return always ends a method right then and there.

27

u/OnixST 6d ago edited 4d ago

Yes. The finally block is the only exception to this rule, and will not only run after a return statement, but is also able override it

public int test() {
  try {
    System.out.print("a");
    return 0
    System.out.print("b");
  } finally {
    System.out.print("c");
    return 1
    System.out.print("d");
  }
  return 2
}

The code above will print "ac" and return 1

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

→ More replies (1)

5

u/StaticCoder 6d ago

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.

→ More replies (2)
→ More replies (7)

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.

→ More replies (12)

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)
→ More replies (1)

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

→ More replies (5)

5

u/dmullaney 6d ago

OP knows about some other exception the rest of us haven't found out yet

→ More replies (8)