在C#中(当使用try/finally时)如何在不调用return的情况下跳转到finally部分.
例如:
public bool SomeMethod()
{
try
{
...
if (...)
{
---> Jump to finally
}
else
{
...
}
...
if (...)
{
---> Jump to finally
}
else
{
...
}
...
return true;
}
finally
{
...
if (...)
return true;
else
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
如何强制跳转到finally部分(不使用return)?
编辑:
我想要做的是:
public bool SomeMethod(ref string Error)
{
try
{
bool result = false;
if (...)
---> Exit to Finally
else
...
...
if (...)
---> Exit to Finally
else
...
...
--> more of the above kind of IFs
return true;
}
finally
{
...
if (!result)
{
LogAMessage("");
...
Error = "...";
...
}
return result;
}
}
Run Code Online (Sandbox Code Playgroud)