在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.
更多推荐
发布评论