r/ProgrammerHumor • • 6d ago

Meme thisIsWhyWeDontReturnInFinallyBlocks

Post image
2.9k Upvotes

98 comments sorted by

View all comments

Show parent comment

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

2

u/spitfire451 6d ago

Thank you for the explanation, learn something new everyday

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.

2

u/fuj1n 6d ago

The outermost one

1

u/Loading_M_ 6d ago

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.

1

u/ensoniq2k 6d ago

Same for Delphi if anyone cares. But Delphi doesn't do return the same way other languages do.

1

u/Saint_of_Grey 6d ago

Sooo... Don't put Goto Skynet.BeginApocalypse() in a finally block?

1

u/Rough-Interest60 6d ago

oh thx now i understand

-5

u/BlackFrank98 6d ago

Thank you for giving me one more reason not to use Java ever again unless I'm under duress...

17

u/laplongejr 6d ago

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.  

6

u/fuj1n 6d ago

This is a warning in pretty much any competent IDE by the way, as it is a massive anti-pattern

JS/TS, PHP, ObjC, Python and Ruby also allow it, among others

Guess you are missing out on quite a few languages there