Why do we use finally blocks?

前端 未结 11 2348
甜味超标
甜味超标 2020-11-27 11:43

As far as I can tell, both of the following code snippets will serve the same purpose. Why have finally blocks at all?

Code A:

try { /*          


        
11条回答
  •  一个人的身影
    2020-11-27 12:21

    finally ALWAYS executes, unless the JVM was shut down, finally just provides a method to put the cleanup code in one place.

    It would be too tedious if you had to put the clean up code in each of the catch blocks.

提交回复
热议问题