Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the C# "finally" block ALWAYS execute? [duplicate]

Tags:

c#

try-catch

Possible Duplicate:
Will code in a Finally statement fire if I return a value in a Try block?

Consider the following code C# code. Does the "finally" block execute?

public void DoesThisExecute() {    string ext = "xlsx";    string message = string.Empty;    try {       switch (ext) {          case "xls": message = "Great choice!"; break;          case "csv": message = "Better choice!"; break;          case "exe": message = "Do not try to break me!"; break;          default:             message = "You will not win!";             return;       }    }    catch (Exception) {       // Handle an exception.    }    finally {       MessageBox.Show(message);    } } 

Ha, after I got done writing this, I realized I could have done tested this myself in Visual Studio. However, please feel free to answer!

like image 800
Ryan Rodemoyer Avatar asked Jul 09 '10 19:07

Ryan Rodemoyer


People also ask

Which is positive C or T?

One coloured line should be in the control line region (C), and another coloured line should be in the test line region (T). Two lines, one next to C and one next to T, even faint lines, show the test is positive.

Is the C on a Covid test negative?

If you have 1 line by C, and 1 line by T this is called a positive result. The lines can be bright or faint. You have COVID-19. If you have 1 line by C, and no line by T this is called a negative result.

What does the C and T mean on the lateral flow test?

This is what the test “window” looks like in the strip after 30 minutes. “C” stands for “control” – this is to make sure the test is working. “T” stands for “test” – this is where your sample result will appear. Image © https://www.gov.uk/ Negative result: one line next to C shows the test is negative.

What is C and T in rapid test?

Positive Result: If you see two lines, Control (C) line and Test (T) line, this means COVID-19 was detected. If positive, please contact your doctor or local health department immediately and follow local guidelines for self-isolation. + 2. Ensure kit is at room temperature for at least 30 minutes prior to use.


2 Answers

No it does not. It will always execute provided the application is still running (except during a FastFail exception, MSDN link, like others noted). It will execute when it exits the try/catch portion of the block.

It will NOT execute if the application crashes: gets killed through a kill process command etc. This is highly important, because if you write code that absolutely expects it to run, like manually doing a roll back, and if not other wise it will automatically commit, you can run into a scenario the application aborts before that happens. Honestly, this is an outside scenario, but it is important to take note of in those situations.

like image 147
kemiller2002 Avatar answered Sep 28 '22 21:09

kemiller2002


From MSDN C# specification of the try statement:

The statements of a finally block are always executed when control leaves a try statement. This is true whether the control transfer occurs as a result of normal execution, as a result of executing a break, continue, goto, or return statement, or as a result of propagating an exception out of the try statement.

Source

There are the cases where the finally block will not execute:

  1. Environment.FailFast
  2. Uncatchable exception types
  3. Power Failure
like image 44
msarchet Avatar answered Sep 28 '22 22:09

msarchet