Windows Phone 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. This post is a part of our Windows Phone tutorial series.

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.

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.

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}">
 <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <!--TitleGrid is the name of the application and page title--> <Grid x:Name="TitleGrid" Grid.Row="0"> <TextBlock Text="Windows Phone 7 Series" x:Name="textBlockPageTitle" Style=" {StaticResource PhoneTextPageTitle1Style}"/> <TextBlock Text="error" x:Name="textBlockListTitle" Style=" {StaticResource PhoneTextPageTitle2Style}"/> </Grid> <Grid x:Name="ContentGrid" Grid.Row="1"> <Border BorderBrush="White"> <TextBlock x:Name="ErrorText" Style=" {StaticResource PhoneTextSmallStyle}" TextWrapping="Wrap" /> </Border> </Grid>

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()

 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
 e.Handled = true; ErrorPage.Exception = e.ExceptionObject; (RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).Source =  new Uri("/ErrorPage.xaml", UriKind.Relative);
Posted by on , in Category Phone with Tags
This post has been submitted by a Guest Author. If you would like to submit a guest post, you may contact me on the mail ID mentioned in the About page.


  1. Raj

    Tried this but an exception gets thrown with the following message

    2024 An error has occurred. [Line: 18 Position: 82]
    The line is in ErrorPage.xaml (System.Windows.Application.LoadComponent(this, new System.Uri(“/WindowsPhoneApplication1;component/ErrorPage.xaml”, System.UriKind.Relative));)

    The code in the app.xaml that runs right before this exception is:
    (RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).Source = new Uri(“/ErrorPage.xaml”, UriKind.Relative);

  2. Raj

    Sorry… figured out the issue.
    When I copied and pasted code from this page the style elements had a space (like so: Style=” {StaticResource PhoneTextPageTitle1Style”)

    turns out Silverlight doesnt like it. But unfortunately no design time error message and the run time error occurs on a line that is different. Made it hard for a NOOB like me.

  3. VT@TWC

    Hi raj. “But unfortunately no design time error message and the run time error occurs on a line that is different”

    thats because spaces are allowed but for this type of code performing it was not expected.. Hope you like it

Leave a Reply

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

4 + 1 =