finally in exception handling

后端 未结 8 1630
甜味超标
甜味超标 2021-01-18 17:08

What exactly does a finally block in exception handling perform?

8条回答
  •  梦毁少年i
    2021-01-18 17:55

    It holds code that should always be executed, regardless of whether an exception occurs.

    For example, if you have opened a file, you should close it in the finally block to ensure that it will always be closed; if you closed it in the try block, an earlier exception would cause execution to jump straight to the catch block and skip closing the file.

    See the Java tutorials for more details.

提交回复
热议问题