Tag: 最后

.NET会停止调试运行finally块中的代码吗?

好吧,我已经阅读(并了解到)finally块并不总是执行其代码(甚至除了拔插头之外)。 仅供参考。有关更多信息,请参阅try catch finally问题 但是,我没有找到: 当我停止调试器时,我的finally块是否被执行? 谢谢!

C#最终执行的时间

拿这个代码: using System; namespace OddThrow { class Program { static void Main(string[] args) { try { throw new Exception(“Exception!”); } finally { System.Threading.Thread.Sleep(2500); Console.Error.WriteLine(“I’m dying!”); System.Threading.Thread.Sleep(2500); } } } } 这给了我这个输出: Unhandled Exception: System.Exception: Exception! at OddThrow.Program.Main(String[] args) in C:\Documents and Settings\username \My Documents\Visual Studio 2008\Projects\OddThrow\OddThrow\Program.cs:line 14 I’m dying! 我的问题是:为什么未处理的exception文本出现在finally的之前? 在我看来,在我们甚至不知道这个exception未处理之前,最终应该在堆栈展开时被解雇。 注意对Sleep()的调用 – 这些是在打印出未处理的exception之后发生的,就像它执行以下操作一样: 未处理的exception文本/消息 […]

在try / catch / finally中“finally”的目的是什么

语法将从语言变为语言,但这是一个普遍的问题。 这有什么区别…. 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.”); 我一直看到它被使用,所以我认为有一个很好的理由最终使用,但我无法弄清楚它是如何只是在语句之​​后放置代码,因为它仍然会运行。 有没有一个场景最终没有运行?

在C#中,如果抛出未处理的exception,最终块将在try,catch中执行。

另一个面试问题是期待一个真/假答案,我不太确定。 重复 在.NET中,如果catch块中的某些内容失败,最终总会被调用? finally块总是运行吗? 最终没有在.net中执行的条件try..finally阻止 如果我在Try块中返回一个值,那么finally语句中的代码会触发吗?