Does it make sense to do “try-finally” without “catch”?

后端 未结 6 1729
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-30 03:42

I saw some code like this:

    try
    {
        db.store(mydata);
    }
    finally
    {
        db.cleanup();
    }

I thought try

6条回答
  •  感动是毒
    2021-01-30 04:28

    It's there because the programmer wanted to make sure that db.cleanup() is called even if the code inside the try block throws an exception. Any exceptions will not be handled by that block, but they'll only be propagated upwards after the finally block is executed. The finally block will also be executed if there was no exception.

提交回复
热议问题