我在理解 try-catch-finally 的执行顺序时遇到问题。我见过的所有示例(例如:http://stackoverflow.com/questions/4191027/order-of-execution-of-try-catch-and-finally-block)都有一个非常简单的“catch”部分打印到控制台。但是如果我在 catch 中使用“throw”语句会发生什么?
我能想到的最简单的代码可以解决问题:
public class TestClass
{
void Foo(int num)
{
int answer = 100;
try
{
answer = 100 / num;
}
catch (Exception e)
{
//Probably num is 0
answer = 200;
throw;
}
finally
{
Console.WriteLine("The answer is: " + answer);
}
}
}
如果 num == 2,那么输出将是:
答案是:50
但是 num == 0 会打印什么?
答案是:100
答案是:200 根本
不打印...
还是只是“未定义的行为”?