表单加载事件中未处理异常 [英] Exception not handled in Form Load Event
问题描述
这个行为是否在所有事件处理程序中发生?这是预期的行为吗?如果是这样,是否因事件处理程序意外停止而发生坏事情的危险性太大?
或者外部VS,当有一个调试器附加到进程时,会发生这种行为。但是,作为调试版本没有任何区别。如果在没有的VS 之外运行调试器,则未处理的异常将会启动。
您可以查看
为什么表单加载不能捕获异常?和
VS2010在64位版本的Windows上的WinForms应用程序中不会显示未处理的异常消息,以获得可能的解决方案。
编辑:据我所知,这种行为只适用于form_load事件处理程序。
I just came across odd behavior with exception handling in .Net. (I'm using C# in MS Visual Studio 2008, but one question I saw here seemed to imply that what I see is true throughout the .Net world.) I am writing a plain WinForm application. I am intentionally causing an unhandled exception to be thrown inside a form_load event handler, outside of any try block. I get no notification. If an unhandled exception occurs in a normal method, a message pops up telling me that the exception happened, and giving me some information about the problem. But in the handler, the code just quietly exits the function without letting anybody know that it happened. If I add a try/catch block, the exception is caught as expected.
Is it true that this behavior happens in all event handlers? And is this expected behavior? And if so, is it because there is too much danger of bad things happening if an event handler unexpectedly stops?
Whether inside or outside VS, this behavior occurs when there is a debugger attached to the process. However, being a debug version makes no difference. If running outside VS without a debugger attached, the unhandled exception will fire up. You can check
Why the form load can't catch exception? , and VS2010 does not show unhandled exception message in a WinForms Application on a 64-bit version of Windows , for possible solutions.
EDIT: This behavior is only specific to the form_load event handler, as far as I know.
这篇关于表单加载事件中未处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!