本文介绍了finally子句不执行并显示输出。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

FInally块没有执行。我有类似的东西。



Hi all,
FInally block is not executing.I have something like this.

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);
}









问题是执行没有进行在事件发生之后最终提醒。





Problem is execution is not going to finally after exception.Thanks in advance.

推荐答案



int i = 123;
            string s = "Some string";
            object o = s;
            File.Create(@"D:\Temp\MyTest1.sql").Close();
            try
            {
                // Invalid conversion; o contains a string not an int
                i = (int)o;
            }
            finally
            {
                File.Create(@"D:\Temp\MyTest2.sql").Close();
                Console.Write("i = {0}", i);
            }





执行代码后,您会看到Temp目录包含2个新文件MyTest1.sql和MyTest12sql。



忘了提,你必须从visual studio构建它,然后双击从文件系统运行它。如果从visual studio运行它,调试模式代码可能无法到达那里。



After executing code you will see Temp directory contain 2 new files MyTest1.sql and MyTest12sql.

Forgot to mention, You must built it from visual studio and then run it from file system with double click. If you run it from visual studio with debug mode code may not reach there.


这篇关于finally子句不执行并显示输出。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 07:37