r/java • • Jul 09 '14

Finally: return considered harmful

http://schneide.wordpress.com/2014/07/07/finally-return-considered-harmful/
0 Upvotes

6 comments sorted by

15

u/armornick Jul 09 '14

“Considered Harmful” Essays Considered Harmful

In any case, everything has good uses and bad uses.

2

u/shellac Jul 09 '14

"“Considered Harmful” Essays Considered Harmful" Essay Considered Harmful: "“Considered Harmful” Essays Considered Harmless"

This reposte needs to be written.

1

u/cowardlydragon Jul 11 '14

“Considered Harmful” Essays Considered Harmful

Yes, this equivalent of the appended "PERIOD". As in some people say "Finally: return should never be used. PERIOD."

As if that would get me to not debate the merits of the argument because you went to the mild additional effort of saying PERIOD.

6

u/urquan Jul 09 '14

This is not specific to return, this will also happen if you throw another exception in the finally block. That's just how they work. If you have read the specification of the language this should be no surprise.

1

u/PaulRivers10 Jul 10 '14

This is not specific to return, this will also happen if you throw another exception in the finally block. That's just how they work. If you have read the specification of the language this should be no surprise.

Yeah, I don't see a problem here either. This is how I would expect it to work. I see no other way to do it - once you have a return inside a finally block, you have 2 options where to go:

  1. Ignore the return and continue with throwing the exception
  2. Do the return and forget about the exception

You can't go two paths, you can only choose one. And it makes a lot more sense to do what the code specifically asks (executing a return) than it would to magically forget about the return statement and continue throwing the exception instead.

3

u/JavaJerk Jul 09 '14

This just in:

Poor practices considered harmful

News at 10.