Exception inside catch block

Possible duplicate:
In C # will the finally block be executed in try, catch, finally, if an unhandled exception is handled?

Finally, will be executed in this scenario (in C #)?

try
{
    // Do something.
}
catch
{
    // Rethrow the exception.
    throw;
}
finally
{
    // Will this part be executed?
}
+3
source share
3 answers

Yes, finally, it always does.

A simple example of demonstrating behavior:

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");
    }
}
+11
source

(Edit: Clarifications from comments are included - thanks guys)
Lastly, always executed. The only exceptions that I know of are:

  • You pull out the power plug
  • , "", , , , , finally . . Joseph Albahari.
  • async, stackoverflows out-of-memory. . .

, , , , , , , .

+3

.

.

, , try/catch, try/finally. .

+1

Source: https://habr.com/ru/post/1708936/


All Articles