tgandrews tgandrews - 2 months ago 6
C# Question

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

try {
// Do stuff
}
catch (Exception e) {
throw;
}
finally {
// Clean up
}


In the above block when is the finally block called? Before the throwing of e or is finally called and then catch?

Answer

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.