C#试题 Try Catch Finally
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace ConsoleApplication1
6{
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 int test = 1;
12 try
13 {
14 test += 1;
15 //whatever throw or not, must execute finally
16 throw new Exception();
17 }
18 catch
19 {
20 test += 1;
21 //firstly must execute finally
22 return;
23 }
24 finally
25 {
26 test += 1;
27 //throw test=4
28 //not throw test=3
29 Console.Write(test);
30 }
31 }
32 }
33}
2using System.Collections.Generic;
3using System.Text;
4
5namespace ConsoleApplication1
6{
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 int test = 1;
12 try
13 {
14 test += 1;
15 //whatever throw or not, must execute finally
16 throw new Exception();
17 }
18 catch
19 {
20 test += 1;
21 //firstly must execute finally
22 return;
23 }
24 finally
25 {
26 test += 1;
27 //throw test=4
28 //not throw test=3
29 Console.Write(test);
30 }
31 }
32 }
33}