How to “catch” unhandled Exceptions

江枫思渺然 提交于 2019-12-08 06:13:14

问题


We've developed a .NET 3.5 CF Application and we're experiencing some application crashes due to unhandled exceptions, thrown in some lib code.

The application terminates and the standard application popup exception message box is shown.

Is there a way to catch all unhandled exceptions? Or at least, catch the text from the message box. Most of our customers simply restart the device, so that we're not able to have a look on the exception message box.

Any ideas?


回答1:


Have you added an UnhandledException event handler?

[MTAThread]
static void Main(string[] args)
{
    AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

    // start your app logic, etc
    ...
}

static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    var exception = (Exception)e.ExceptionObject;

    // do something with the info here - log to a file or whatever
    MessageBox.Show(exception.Message);
}



回答2:


I do something similar to what ctacke does.

private static Form1 objForm;

[MTAThread]
static void Main(string[] args)
{
  objForm = new Form1();
  try
  {
    Application.Run(objForm);
  } catch (Exception err) {
    // do something with the info here - log to a file or whatever
    MessageBox.Show(err.Message);
    if ((objForm != null) && !objForm.IsDisposed)
    {
      // do some clean-up of your code
      // (i.e. enable MS_SIPBUTTON) before application exits.
    }
  }
}

Perhaps he can comment on whether my technique is good or bad.



来源:https://stackoverflow.com/questions/14937863/how-to-catch-unhandled-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!