Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditions when finally does not execute in a .net try..finally block

Basically I've heard that certain conditions will cause .net to blow past the finally block. Does anyone know what those conditions are?

like image 613
NotMe Avatar asked Sep 21 '08 18:09

NotMe


People also ask

In which condition finally block is not executed C#?

The only reason a finally block won't get executed is if the thread is terminated abnormally (other than a corrupted stack--but the MDAs should catch that).

Under what conditions is the finally block executed?

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.

Does a Finally block always get executed in C#?

A finally block always executes, regardless of whether an exception is thrown.

How do I make finally block not execute?

exit() will prevent a finally block from executing.


2 Answers

Two possibilities:

  • StackOverflowException
  • ExecutionEngineException

The finally block will not be executed when there's a StackOverflowException since there's no room on the stack to even execute any more code. It will also not be called when there's an ExecutionEngineException, which may arise from a call to Environment.FailFast().

like image 109
Haacked Avatar answered Oct 05 '22 01:10

Haacked


Unless the CLR blows up and goes down with an ExecutingEngineException (I've seen a few in the .net 1.1 days with just the right amount of COM Interop :) .. I think finally should always execute.

like image 38
Gishu Avatar answered Oct 04 '22 23:10

Gishu