In C# (when using a try / finally) how can I jump to the finally section without calling return.
For example:
public bool SomeMethod()
{
try
{
...
if (...)
{
---> Jump to finally
}
else
{
...
}
...
if (...)
{
---> Jump to finally
}
else
{
...
}
...
return true;
}
finally
{
...
if (...)
return true;
else
return false;
}
}
How can I force a jump to the finally section (without using return)?
Edit:
What I'm trying to do is:
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;
}
}
You could use a label and goto
.
However, this seems to suggest that your function is probably not structured well.
In this case, you seem to be using finally
as a function - why not extract that logic into a function of its own?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With