Finally: guaranteed to be called anyway

Is there even a small chance that finallyit will not be called, but the application is still working?

I release a semaphore there

        finally
        {
            _semParallelUpdates.Release();
        }

and they’re afraid of losing some of them.

+3
source share
6 answers

If you are looking for ways to make your code reliable, I suggest reading the following article:

Reliability Recommendations>

Another great article recommended by VinayC is this:

Stephen Tuub: Save Your Code with .NET Framework Reliability Features

0
source

, . CriticalFinalizerObject SafeHandle . , , , . , , , .

, , , (, ). , , , , . . , - (, ), .

+1

Windows- . finally .

0

Framework 1.0 1.1 , , finally, Thread.Abort, .

0

, finally? ( - , , ... , , ).

, , , , !

, :

IDisposable someDisposableObject = null;
IDisposable someOtherDisposableObject = null;

try
{
    someDisposableObject = GetDisposableObject();

    throw new Exception("Holy crap, something bad happened.");

    someOtherDisposableObject = GetOtherDisposableObject();
}
finally
{
    // This will throw a NullReferenceException...
    someOtherDisposableObject.Dispose();

    // ...so this actually won't run.
    someDisposableObject.Dispose();
}

, , finally , , () .

0
source

The current thread does not leave the current stack stack until the finally block is executed or the finally block is thrown or an exception from the finally block is executed. If a thread dies or is blocked in a try block, execution never leaves the current stack stack, but it will also not execute the final block.

0
source

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


All Articles