How to skip finally block in C#?
Is it possible to skip the 'finally' block even though its present after a try And catch block.
How to skip finally block in C#?
Is it possible to skip the 'finally' block even though its present after a try And catch block.
The only way I can think of is a flag:
bool skipFinally = false;
try
{
DoSomething();
skipFinally = true;
DoSomethingElse();
}
finally
{
if (!skipFinally)
DoFinallyStuff();
}
So the finally
block literally gets executed, but you can decide what to do there.
But it has the smell of a design flaw. Why do you have code in a finally
block that possibly should not be executed in the first place?
But on the other hand: the C# compiler generates code like above for iterator blocks.