How to “catch” unhandled Exceptions

核能气质少年 提交于 2019-12-07 10:00:24

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);
}
jp2code

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.

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