跳到最后没有调用返回

mas*_*mas 2 c#

在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)

Ode*_*ded 6

你可以使用标签和goto.

但是,这似乎表明您的功能可能没有很好的结构.

在这种情况下,您似乎将其finally用作函数 - 为什么不将该逻辑提取到自己的函数中?