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.
A finally block always executes, regardless of whether an exception is thrown.
The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.
A finally block is always get executed whether the exception has occurred or not. If an exception occurs like closing a file or DB connection, then the finally block is used to clean up the code. We cannot say the finally block is always executes because sometimes if any statement like System.
When the System. exit() method is called in the try block before the execution of finally block, finally block will not be executed.
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.
If the power is lost (for instance, if you unplug the computer and it has no battery and is not connected to a UPS), it is very possible that the finally
block will not be run. A major OS or driver malfunction (such as a BSOD) might also cause this. However, the entire idea with the try..finally
construct is that the finally
block is to be run even if an exception (of any kind) is raised inside the try
block. The finally
block will even run if there is an exit
statement inside try
block.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With