Search code examples
delphiexceptiontry-catchdelphi-2006finally

Delphi - try finally block is guaranteed by compiler to be executed correctly?


I know this was discussed on other topics also, what I'm asking is exactly the title of this question.

Is there such case when try/finally the finally won't execute?

 try
  //some error here
 finally
  //code that MUST be executed
 end;

I'm not talking about how try..except/finally blocks must be used, I'm just asking if this could happen.

LE: Application.Terminate/unplug your computer are particular cases.


Solution

  • try..finally guarantees that code in the finally block will execute regardless of any exception occuring in the protected block. This of course doesn't apply if the process is killed before the finally block can execute, e.g. by TerminateProcess or turning the power off. An endless loop in the protected block may also prevent the finally block from executing.