Returning from a finally block in Java

前端 未结 6 1928
醉梦人生
醉梦人生 2020-11-22 11:02

I was surprised recently to find that it\'s possible to have a return statement in a finally block in Java.

It seems like lots of people think it\'s a bad thing to d

6条回答
  •  难免孤独
    2020-11-22 11:40

    javac will warn of return in finally if you use the -Xlint:finally. Originally javac emitted no warnings - if something is wrong with the code, it should fail to compile. Unfortunately backwards compatibility means that unanticipated ingenious foolishness cannot be prohibited.

    Exceptions can be thrown from finally blocks, but in that case the exhibited behaviour is almost certainly what you want.

提交回复
热议问题