1

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?

ctacke
  • 66,480
  • 18
  • 94
  • 155
Chris
  • 2,296
  • 4
  • 27
  • 46

2 Answers2

6

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);
}
ctacke
  • 66,480
  • 18
  • 94
  • 155
  • Amazing! I added this to some of my project that have not been showing any signs of errors, and this thing is catching Exceptions I never even knew were going on. –  Feb 25 '13 at 02:45
0

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.

Community
  • 1
  • 1
  • 2
    This will often miss exceptions in worker threads and native exceptions. An AppDomain.UnhandledException handler tends to catch more stuff. – ctacke Feb 19 '13 at 15:08