I'm looking for a keyword or clean way to exit a finally block without using selection statements. Consider the following example:
private bool AtteptToDoSomething()
{
try
{
MethodThatCouldFail();
return true;
}
catch
{
return false;
}
finally
{
//Jump out of function here?
//... to avoid executing this under certain conditions
DoSomethingFinalizing();
}
}
Attempts that didn't work:
finally
{
return;
break;
continue;
goto ExitSymbol;
//This works, but would require a second try-catch block
throw new Exception();
}
The compiler errors for above examples include:
Control cannot leave the body of a finally clause
and
Cannot jump out of the finally block
It's pretty clear now that a finally block can't transfer control by any regular means.
There is a pretty good explanation why this won't work with continue, but unfortunately not why this can't be done in any other case.
Are there any (CLR or C# based) reasons why this is not allowed/possible?
Are there any exceptions to this restriction except for throw?