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!
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.
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.
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.
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.
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.
From MSDN C# specification of the try
statement:
The statements of a
finally
block are always executed when control leaves atry
statement. This is true whether the control transfer occurs as a result of normal execution, as a result of executing abreak
,continue
,goto
, orreturn
statement, or as a result of propagating an exception out of thetry
statement.
Source
There are the cases where the finally block will not execute:
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