WP7 Tutorial 6 : Managing Errors in the Application

In this tutorial, you will learn how to update the application to display an error page whenever an Unhandled Exception occurs in the application. The task shows how to handle the errors as well as how to add pages to your application and navigate between them.
1. Add a new page to the project. In Solution Explorer, right-click the HelloPhone project option, and Add New Item. In the Add New Item dialog, select Windows Phone Portrait Page from the list of templates given, set the name to ErrorPage.xaml and then click Add.
error1 600x381 WP7 Tutorial 6 : Managing Errors in the Application
2. In ErrorPage.xaml, locate the LayoutRootGrid element and replace its child controls with the blue-highlighted XAML markup. This XAML defines an application title and a page title, both named error. It also defines a TextBlock object designated as x:Name=”ErrorText” that will hold the error text from any future exceptions.
...

  
    
    
  
  
  
    
    
  
  
    
      
    
  

...
3. Press F7 to open the code-behind file of the new page or, right-click ErrorPage.xaml in Solution Explorer and select View Code. Then, insert the following code snippet into the ErrorPage class at the highlighted location. This sets up an Exception object that is tied up to the ErrorText.
public partial class ErrorPage : PhoneApplicationPage
{
  public ErrorPage()
  {
    InitializeComponent();
  }

  public static Exception Exception;
  // Executes when the user navigates to this page.
  protected override void OnNavigatedTo(Microsoft.Phone.Navigation.PhoneNavigationEventArgs e)
  {
    ErrorText.Text = Exception.ToString();
  } 
}
4. After that , hook up an event handler to navigate to the error page and display an error message whenever an unhandled exception occurs. In Solution Explorer, right-clickApp.xaml and select View Code to open the code-behind class of the Application class.
5. Find the Application_UnhandledException event handler and insert the following code snippet at the highlighted location immediately before the closing brace. TheApplication_UnhandledException is a safety net where all unhandled exceptions of your application end up. Now you hook up the exception object to the ErrorPage.Exceptionobject and when you browse to the error page, it takes the exception object’s text value (Exception.ToString();) and displays it on the page. This will be very useful once you start debugging your application on a real device.
// Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
  if (System.Diagnostics.Debugger.IsAttached)
  {
    // An unhandled exception has occurred, break in the debugger
    System.Diagnostics.Debugger.Break();
  }
  e.Handled = true;
  ErrorPage.Exception = e.ExceptionObject;
  (RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).Source = 
      new Uri("/ErrorPage.xaml", UriKind.Relative);
}

Comments