Carlos Liu
Carlos Liu

Reputation: 2438

Exception won't be caught if I don't debug the application

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?

Upvotes: 3

Views: 252

Answers (1)

Andrew Savinykh
Andrew Savinykh

Reputation: 26280

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.

Upvotes: 3

Related Questions