When is finally run if you throw an exception from the catch block?

后端 未结 7 1549
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 05:06
try {
   // Do stuff
}
catch (Exception e) {
   throw;
}
finally {
   // Clean up
}

In the above block when is the finally block called? Before the

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 05:17

    It would be called after e is re-thrown (i.e. after the catch block is executed)

    editing this 7 years later - one important note is that if e is not caught by a try/catch block further up the call stack or handled by a global exception handler, then the finally block may never execute at all.

提交回复
热议问题