【发布时间】:2013-05-18 06:28:46
【问题描述】:
语法会因语言而异,但这是一个普遍的问题。
这有什么区别....
try
{
Console.WriteLine("Executing the try statement.");
throw new NullReferenceException();
}
catch (NullReferenceException e)
{
Console.WriteLine("{0} Caught exception #1.", e);
}
finally
{
Console.WriteLine("Executing finally block.");
}
还有这个……
try
{
Console.WriteLine("Executing the try statement.");
throw new NullReferenceException();
}
catch (NullReferenceException e)
{
Console.WriteLine("{0} Caught exception #1.", e);
}
Console.WriteLine("Executing finally block.");
我不断看到它被使用,所以我认为使用 finally 是有充分理由的,但我无法弄清楚它与仅在语句之后放置代码有何不同,因为它仍然会运行。
有没有 finally 不运行的场景?
【问题讨论】:
-
..这是stackoverflow.com/questions/3216046/… 的可能重复项,stackoverflow.com/questions/345091/… 的可能重复项是此问题的最佳答案。嗯,不错,甜美又短。
标签: c# javascript exception-handling finally