I have been reading about try/finally on MSDN and found out following code. They say it WILL run the Finally clause but when I run it, it just does not continue and does not run it. What is the problem?
public class TestTryFinally
{
public static void Main()
{
int i = 123;
string s = "Some string";
object o = s;
try
{
// Invalid conversion; o contains a string not an int
i = (int)o;
}
finally
{
Console.Write("i = {0}", i);
}
}
}
Looks fine to me. It will indeed run the finally block. My guess is that the debugger is popping up a dialog box for you, and you’re not managing to force execution to continue.
Run it not under the debugger – that will avoid the debugger getting in the way as it tries to “help” you.