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

后端 未结 6 1576
爱一瞬间的悲伤
爱一瞬间的悲伤 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:20

    If any of the code in the try block can throw a checked exception, it has to appear in the throws clause of the method signature. If an unchecked exception is thrown, it's bubbled out of the method.

    The finally block is always executed, whether an exception is thrown or not.

提交回复
热议问题