1            try
 2             {
 3                 int i = Convert.ToInt32(Console.ReadLine());
 4                 Console.WriteLine("in the 'try'");
 5             }
 6             catch
 7             {
 8                 Console.WriteLine("in the 'catch'");
 9             }
10             finally
11             {
12                 Console.WriteLine("in the 'finally'");
13             }
14             Console.WriteLine("After the 'finally'");
15             Console.ReadKey();

对2种情况进行测试

1:try中的语句执行中有异常

2:try中的语句执行中没有异常

第1种情况:

输入999999999999999999

执行结果为

999999999999999999
in the 'catch'
in the 'finally'
After the 'finally'

分别执行了catch 、finally、finally后的代码

 

第2种情况:

输入123

执行结果为

123
in the 'try'
in the 'finally'
After the 'finally'

分别执行了try、finally、finally后的代码

 

结论:1.try中的代码执行有异常时,其(try中有异常的代码)后面的代码不执行,执行catch中的代码

   2.try中的代码执行没有异常时,catch中的代码不执行

     3.finally中的代码和try...catch..finally后的代码总是要执行

 

  

 

posted on 2013-06-30 21:41  瑾秀年华  阅读(485)  评论(1)    收藏  举报