如果我不调试应用程序,将不会捕获异常(Exception won't be caught if I don't debug the application)

编程入门 行业动态 更新时间:2024-10-28 00:21:01
如果我不调试应用程序,将不会捕获异常(Exception won't be caught if I don't debug the application)

在A.DLL中有两个DLL,A和B,有一个这样的表单类:

namespace AAA public class AForm: Form { ... private void btnOK_Click(object sender, EventArgs e) { DoSomeSth(); } }

在B.DLL中,有一些像这样的代码

try { AForm dlg = new AAA.AForm(); dlg.ShowDialog(); } catch(Exception ex) { MessageBox.Show(ex.Message); }

单击OK按钮后,函数DoSomeSth中会出现异常当我使用vs2005调试代码时,异常可以在B.DLL中捕获,但是如果我直接运行应用程序而没有调试,则不会在B中捕获异常.DLL,是什么原因?

There are two DLLs, A and B, in A.DLL there is a form class like this:

namespace AAA public class AForm: Form { ... private void btnOK_Click(object sender, EventArgs e) { DoSomeSth(); } }

In B.DLL, there is some code like this

try { AForm dlg = new AAA.AForm(); dlg.ShowDialog(); } catch(Exception ex) { MessageBox.Show(ex.Message); }

There will be exception in the function DoSomeSth after OK button is clicked When I debug the code using vs2005, the exception could be caught in B.DLL, but if I ran the application directly without debug, the exception won't be caught in B.DLL, what is the reason?

最满意答案

这是一个已知问题 。 根据知识库文章:

在不使用调试器的情况下运行Windows窗体应用程序时,可以使用NativeWindow.CallBack方法捕获异常并防止程序意外退出(崩溃)。 在NativeWindow.CallBack方法中,使用标准异常对话框填充异常消息。

但是,如果使用调试器运行Windows窗体应用程序,则不会捕获异常,因为您使用NativeWindow.DebuggableCallBack方法。 当您使用NativeWindow.DebuggableCallBack方法时,实时(JIT)调试器会停止应用程序的运行。

This is a known issue. According to the KB article:

When you run your Windows Forms application without using the debugger, you use the NativeWindow.CallBack method to catch the exception and to prevent the program from unexpectedly quitting (crashing). In the NativeWindow.CallBack method, you populate the exception message by using a standard exception dialog box.

However, if you run your Windows Forms application with the debugger, you do not catch the exception because you use the NativeWindow.DebuggableCallBack method. When you use the NativeWindow.DebuggableCallBack method, the just-in-time (JIT) debugger stops the application from running.

更多推荐

本文发布于:2023-07-16 00:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1120980.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我不   应用程序   异常   Exception   debug

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!