Global Error Handling

The default global error handler in the WP7 project template is really not all that helpful:

 private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // An unhandled exception has occurred; break into the debugger
                System.Diagnostics.Debugger.Break();
            }
        }

Sure, it’s fine during development.  But do you know what happens to your app in production if there is an unhandled exception?  Your app crashes and exits with no explanation to the user, and no feedback to you to prevent it from happening again.  I guess the idea is that you’ll find & handle every possible exception during development.  Chances of that happening on an app of any complexity at all:  0%.  Interesting to note that an app which crashes & exits this way violates the Marketplace submission guidelines.

Here is one alternative:

private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e){
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // An unhandled exception has occurred; break into the debugger
                System.Diagnostics.Debugger.Break();
            }
            else
            {
                OnAnyError(e.ExceptionObject);
            }
            e.Handled = true;  //optional
}

private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e){
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // A navigation has failed; break into the debugger
                System.Diagnostics.Debugger.Break();
            }
            else
            {
                OnAnyError(e.Exception);
            }
            e.Handled = true; //optional
}

private void OnAnyError(Exception e){
            if (MessageBox.Show("May I send the details of this error to the developer?  That way they can fix it so it doesn't happen again.  I'll show you the entire message before it's sent.", "Application error", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                EmailComposeTask emailComposeTask = new EmailComposeTask();
                emailComposeTask.To = "[app]@[dev].com";
                emailComposeTask.Subject = "An error in [app] " + VersionNumber;
                emailComposeTask.Body = e.ToString();
                emailComposeTask.Show();
            }
}

This does a few things:

  1. Instantly lets your users know you care,  and there is a strong possibility you will fix the problem they encountered.
  2. Prompts your user to communicate with you, without doing so anonymously. This way you can engage in further dialogue about the cause of their error (or possibly your app in general).
  3. Gives you the REAL information you need to fix the problem.  Because if your app doesn’t send you a stack trace, you tend to get an email or tweet with “your app crashed”, and then you are stuck in blind debugging hell.
  4. Improve your code, get feedback from users in real-world situations.

Notice also the message asks permission to initiate contact, which I think is important.

I am not ashamed to say I have received a few of these emails for DataHub.  Every email I received I replied to individually without using a form letter.  The structure was something like this:

[user name],
Thank you so much for sending your error report.  I have been able to reproduce your problem and it will be fixed in our upcoming [#] release.  In the meantime, the workaround is to [insert details].

Regards,
Jason

And then I have gotten multiple replies like this:

Wow! That’s the best support from a dev I have experienced in a while, thanks for replying so quickly.
Happy customer here! :D

Wow – thanks Jason! Didn’t expect a response at all.
=)

Ceci mis à part : super software !!!
Très cordialement

Hi Jason
I can’t recreate it either now. [more details]
Hope that helps

thanks for app

And reviews like:

Great app, amazing support…

Any question you might have will be answered in no time by a very friendly and committed support. I am looking forward to the desktop version of the app. Great app, great dev…

…and the developpers team seems to be really motivated.

Regards,
Jason

This entry was posted in WP Dev Tips. Bookmark the permalink.

2 Responses to Global Error Handling

  1. Aiman Afzak says:

    How to apply the same fix for the Windows8?

Leave a Reply

Your email address will not be published. Required fields are marked *

*



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>