表单加载事件中未处理异常

表单加载事件中未处理异常

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

问题描述

我刚刚在.Net中遇到异常处理的奇怪行为。 (我在MS Visual Studio 2008中使用C#,但我在这里看到的一个问题似乎意味着我在整个.Net世界中看到的是真实的)。我正在编写一个简单的WinForm应用程序。我有意造成一个未处理的异常被抛出在form_load事件处理程序之外,在任何try块之外。我没有通知。如果在正常方法中发生未处理的异常,则会弹出一条消息,告诉我异常发生,并给出一些有关该问题的信息。但是在处理程序中,代码只是静静地退出函数,而不让任何人知道它发生了。如果我添加了一个try / catch块,那么异常被按预期的方式被捕获。



这个行为是否在所有事件处理程序中发生?这是预期的行为吗?如果是这样,是否因事件处理程序意外停止而发生坏事情的危险性太大?

解决方案

或者外部VS,当有一个调试器附加到进程时,会发生这种行为。但是,作为调试版本没有任何区别。如果在没有的VS 之外运行调试器,则未处理的异常将会启动。
您可以查看




,以获得可能的解决方案。



编辑:据我所知,这种行为只适用于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? , andVS2010 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.

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

09-01 19:24