Reporting unhandled exceptions in your Windows Phone apps
While many bugs are caught during development, nothing tests an app as much as real-world testing on a deployed application. At this point it is important to be able to get debug information back from the end-user. This article collects links to other public topics on how to catch unhandled exceptions in your app, log them, and send them to the developer for analysis.
Article Metadata
Code Example
Tested with
Compatibility
Article
Basic methods
There are two basic methods for getting information about exception conditions that occur in an app:
- Detect when an exception occurs and save the extended error information.
- There is a method named Application_UnhandledException() in app.xaml.cs file which gets called whenever an unhandled exception gets fired. Extended exception information like the stack trace can be written to a crash log here:
-
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
string errorString = DateTime.Now.ToLocalTime().ToString() + " | " + "Message: " + e.ExceptionObject.Message + "Stack Trace: " + e.ExceptionObject.StackTrace;
WriteCrashLog(errorString);
}
- Log data at checkpoints in code. This allows you to verify "last good" points in your code execution and get some information about the state of the running app
Once an error log has been created, there are several ways to make this available to the developer:
- Create a log file in isolated storage and send to a server or to a specified email address
- Display error data in a special page in the app next time it is started
Article list
Various approaches based on the above methods are well documented on the Internet. The following articles are recommended reading:
- Handling Exceptions on Windows Phone 7 (Jimmy's Blog)
- Exception Class (MSDN)
- Handling and Throwing Exceptions (MSDN)
- Managing Errors in a Windows Phone Silverlight Application (zorn consulting blog)
- Error Reporting on Windows Phone 7 (Andy Pennell's Blog)
- Windows Phone 7 Error Handling, Reporting (geoffhudik blog)
- Search on Exception handling (google)
Example code
The attached example is a modification of Listbox handling in Windows Phone 7 in which listBoxFlickerSearch_SelectionChanged() creates an unhandled exception when a user clicks on the list item. The exception is then displayed in its own view. This approach is very similar to that described in Jimmy's Blog above.
The full source code of the example is available here: File:ErrorHandler.zip


Hamishwillee - Reviewed/fixed
Hi Thomas
Tidied up the English a bit - wasn't awful, so mostly a matter of style. Moved the introduction into the abstract section up the top - which I think is now more succinct and avoids duplication.
Lastly, added in small code fragment, which I think is useful "hint" for users and search engines.
Thanks for your work on this (also Vinay!)
Regards H
PS, also fixed titlehamishwillee 09:58, 26 April 2013 (EEST)