Is it valid to have finally block without try and catch?

前端 未结 11 1590
遇见更好的自我
遇见更好的自我 2021-02-04 01:32

I am trying to use the finally block without using the try/catch blocks but getting the error in Eclipse.

Can I use the finally block without using the try/catch blocks?

11条回答
  •  暖寄归人
    2021-02-04 02:05

    The code within the finally block is guaranteed to execute if program control flow enters the corresponding try block. So it makes no sense to have a finally without a try.

    The only exception to this is if the program encounters a System.exit(...) call before the finally block, as that shuts down the virtual machine.

提交回复
热议问题