C#试题 Try Catch Finally
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
namespace 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
}
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace ConsoleApplication16
{7
class Program8
{9
static void Main(string[] args)10
{11
int test = 1;12
try13
{14
test += 1;15
//whatever throw or not, must execute finally16
throw new Exception();17
}18
catch19
{20
test += 1;21
//firstly must execute finally22
return;23
}24
finally25
{26
test += 1;27
//throw test=428
//not throw test=329
Console.Write(test);30
}31
}32
}33
}



浙公网安备 33010602011771号