Delphi - finally, is the finally block guaranteed that the compiler will execute correctly?

I know that this was discussed on other topics that I ask - this is the name of this issue.

Is there such a case where try / finally finally fails?

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

I'm not talking about how to use try..except / finally, I'm just asking if this could happen.

LE: Application. Terminal / computer disconnection are special cases.

+6
source share
4 answers

try..finally guarantees that the code in the finally block will be executed regardless of any exception that occurs in the protected block. This, of course, does not apply if the process is killed before the finally block can execute, for example. on TerminateProcess or turn off the power. An endless loop in a protected block can also prevent the execution of the finally block.

+22
source

If the power is lost (for example, if you disconnected the computer and it does not have a battery and is not connected to the UPS), it is very possible that the finally block will not start. A serious malfunction of the OS or drivers (for example, BSOD) can also occur. However, the whole idea with the try..finally construct is that the finally block must be executed, even if an exception (of any kind) is thrown in the try block. The finally block will work even if there is an exit try in the try block.

+4
source

If your application raises a DEP (Data Execution Prevention) exception, I don't think windows will let you continue. Your process will be confused without completing the final section. Your process just β€œleaves”. However, this has nothing to do with what the compiler did or did not do.

+3
source

After you enter try /, the finally block will be executed until try / finally completes.

+1
source

Source: https://habr.com/ru/post/895879/


All Articles