【发布时间】:2011-02-21 03:10:49
【问题描述】:
我使用这段代码来捕获 WinForm 应用程序 UnhandledException。
[STAThread]
static void Main(string[] args)
{
// Add the event handler for handling UI thread exceptions to the event.
Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
// Set the unhandled exception mode to force all Windows Forms errors
// to go through our handler.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// Add the event handler for handling non-UI thread exceptions to the event.
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
try
{
Application.Run(new MainForm());
} catch....
我将尝试重新启动应用程序。现在我的问题是模拟这样的异常。我在尝试之前尝试过(主要):throw new NullReferenceException("test"); VS 抓住了它。
在带有按钮的 MainForm 代码中也试过:
private void button1_Click(object sender, EventArgs ev)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(TestMe), null);
}
protected void TestMe(object state)
{
string s = state.ToString();
}
没有帮助,VS 抓住了它,即使在发布模式下也是如此。
- 最后我应该如何强制应用程序生成
UnhandleldException? - 我能否重新启动
CurrentDomain_UnhandledException中的应用程序? - 如何生成
ThreadException?
PS。
如果我在 VS 之外启动 一个 windows 通用窗口
Application MyApplication”遇到 一个错误,应该是 关闭...blabla...发送报告/不要 发送。
不过我想VS进入这个方法(...Domain_Unhahdled...)
编辑: 重新启动应用程序时,是否可以 禁用 Windows 崩溃消息,如下所示: alt text http://byfiles.storage.msn.com/y1pOhWnAAXfMYtJH2VNa5iL0l1hjAqNHD2VmVnl8nN6L1oQC_xHkyHCJxhMc1ZLxLOH9ZXfZoo5zX8?PARTNER=WRITER ?
代码:
static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
// Since we can't prevent the app from terminating
// log this to the event log.
Logger.LogMessage(ERROR, errorMsg);
Application.Restart();
【问题讨论】:
-
我不确定我是否理解这个问题...您是在问为什么即使您注册了全局处理程序,Visual Studio 也会中断异常?
-
@Aaronaught:我想进入 CurrentDomain_UnhandledException 方法(我将尝试测试重启应用程序代码),最后我的应用程序死掉了。
-
请澄清您的意思,“VS 抓住了它”
-
@Rising Star: VS 打破它并且不进入 CurrentDomain_UnhandledException。
-
@serhio: 当你恢复执行,或者当你在 VS 调试器之外运行程序时会发生什么?
标签: .net winforms testing exception-handling throw