catch区内的异常

nia*_*her 3 c#

可能重复:
在C#中,finally块会在try,catch中执行,最后是否会抛出未处理的异常?

最终会在这种情况下执行(在C#中)?

try
{
    // Do something.
}
catch
{
    // Rethrow the exception.
    throw;
}
finally
{
    // Will this part be executed?
}
Run Code Online (Sandbox Code Playgroud)

Fre*_*örk 11

是的,最后总是执行.

演示行为的简单示例:

private void Button_Click(object sender, EventArgs e)
{
    try
    {
        ThrowingMethod();
    }
    catch
    { 
    }
}

private void ThrowingMethod()
{
    try
    {
        throw new InvalidOperationException("some exception");
    }
    catch
    {
        throw;
    }
    finally
    {
        MessageBox.Show("finally");
    }
}
Run Code Online (Sandbox Code Playgroud)