Does a finally block always get executed in Java?

前端 未结 30 2271
逝去的感伤
逝去的感伤 2020-11-21 07:24

Considering this code, can I be absolutely sure that the finally block always executes, no matter what something() is?

try         


        
30条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-21 07:36

    Also, although it's bad practice, if there is a return statement within the finally block, it will trump any other return from the regular block. That is, the following block would return false:

    try { return true; } finally { return false; }
    

    Same thing with throwing exceptions from the finally block.

提交回复
热议问题