表单加载事件中未处理的异常 [英] Exception not handled in Form Load Event

查看:22
本文介绍了表单加载事件中未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在 .Net 中遇到了异常处理的奇怪行为.(我在 MS Visual Studio 2008 中使用 C#,但我在这里看到的一个问题似乎暗示我所看到的在整个 .Net 世界中都是正确的.)我正在编写一个普通的 WinForm 应用程序.我故意在任何 try 块之外的 form_load 事件处理程序中引发未处理的异常.我没有收到任何通知.如果在正常方法中发生未处理的异常,则会弹出一条消息,告诉我发生了异常,并提供有关该问题的一些信息.但是在处理程序中,代码只是悄悄地退出函数,而不让任何人知道它发生了.如果我添加一个 try/catch 块,异常会按预期捕获.

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?

推荐答案

无论是在VS内部还是外部,当有调试器附加到进程时,就会出现这种行为.但是,作为调试版本没有区别.如果在 VS 之外运行 没有附加调试器,未处理的异常将触发.你可以检查

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

为什么表单加载无法捕获异常? ,和VS2010 不显示未处理的异常在 64 位版本的 Windows 上的 WinForms 应用程序中显示消息,以获取可能的解决方案.

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.

据我所知,此行为仅特定于 form_load 事件处理程序.

This behavior is only specific to the form_load event handler, as far as I know.

这篇关于表单加载事件中未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆