19 July 2012

Exception Handling in Windows Phone 7 - When all else fails

I was recently testing something with Toast Notifications with Windows Phone 7.1. Visual Studio that I use has amazing debugging features like any other IDE available IDE out there. Here was scenario. I need to send two types of Notifications Raw and Toast Notifications to my mobile and need to assert scenarios. Debugging the Raw Notification was very simple. However, I was not able to debug/step into when I launch the application after clicking the toast Notifcation.

It was really painful not to be able see any exception message. It was very annoying. Then I found a way to at least know what is happening. To do that, you can just go the App.xaml.cs file which has all the main entry and exit points to the application. To read more about this, just google life cycle of a windows phone app.

All I had to do was add a message box to the method Application_UnhandledException in the App.xaml.cs.

private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
    MessageBox.Show(e.ExceptionObject.Message + "\n" + e.ExceptionObject.StackTrace);
    if (System.Diagnostics.Debugger.IsAttached)
        // An unhandled exception has occurred; break into the debugger

Problem Solved. Now I could see the error messages/exception stack trace in a neat Message box!


No comments: