Generally, when we tend to see the exception, there should be some sort of useful and meaningful detail or message displayed to the user that needs to be provided to the developer. This will assist the developer in finding the root cause of the issue or rather we should debug the issue. The bad thing or we could say the worst thing that could happen in such cases is the crash of the application and just get exit from it.
The crash of the application can be really irritating to the user and stop the end user to further use the same and can lead to that application being less popular. The example used in this article will make sure that the navigation is routed to the error page in case of the occurrence of an exception. But this is not the best solution to be displayed to the end user. The strategy should be such that a more generic message is displayed to the user giving him the option to notify the error which in turn will send an email to the respective address containing the exception details.
Step1. One needs to add a new Windows Phone Portrait Page to the Windows Phone 7 project in Visual Studio. Here in, we will be making use of the example naming the page as ‘Error.xaml’.
Step2. In ‘Error.xaml’, identify the ‘LayoutRoot’ Grid element, and then the task is to replace its content with the subsequent content:
<!--TitlePanel contains the application name and the page label-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12">
<TextBlock x:Name="Title of the Application" Text="New Application"
<TextBlock x:Name="PageLabel" Text="error" Margin="-3,-8,0,0"
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1">
Step3. Now when we have created the page, it will behave as the container for any sort of exceptions that we would be displaying across.
The next thing that needs to be done is to open up ‘Error.xaml.cs’, and add the subsequent code:
public static Exception Exception;
// This is implemented when the user is routed to this page.
protected override void OnNavigatedTo(NavigationEventArgs e)
ErrorText.Text = Exception.ToString();
Now, an exception object is being set up that is hooked up to ErrorText.Text on routing to the page.
Step4. The last step would be to catch an event handler that would make certain we routed tso this page at the time when an exception occurs that is unhandled.
This is made possible from ‘App.xaml.cs’. One needs to Open up‘App.xaml.cs’, and then the content of the ‘Application_UnhandledException’ function needs to be replaced with the subsequent code:
// An unhandled exception has occurred; break in the debugger
// Executing on a emulator or device without the need of debugging
e.Handled = true;
Error.Exception = e.ExceptionObject;
(RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).Source =
new Uri("/Error.xaml", UriKind.Relative);
In case the above steps are completed without any issue being observed, then the moment an unhandled exception happens in your application, the application will be routed to this page and the exception stack trace will be displayed.
The verification of these steps would involve adding a button to the ‘MainPage.xaml’ and then get it linked to a page that never exists. Below is the example of the same:
NavigationService.Navigate(new Uri(“/NotHere.xaml”, UriKind.Relative));
The application needs to be launched now in the emulator and then the button needs to be clicked that is just created, (This needs to be launched without debugging). The outcome should be rerouted to ‘Error.xaml’ with the details of the exception.
Figure 1: Showing error exception
Assuming we need to test something with Toast Notifications with Windows Phone 7.1. The Visual Studio that we normally make use of debugging features that are just outstanding. The features could be similar to any other IDE available out there. Let us see the scenario we are going to execute over here.
We need to send two sort of Notifications over here- one is Raw and the other would be toast Notifications to the mobile where we would be required to assert the scenarios. It is not difficult to debug the Raw Notification but it would not be easy to do the same when the application is launched after accessing the toast Notification.
In order to do this, one can just go the App.xaml.cs file that contains all the entry and exit points to the application. All you need to do is to 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);
// An unhandled exception has occurred; break into the debugger
The article concentrates on the error handling mechanisms for the Windows 7 phone applications and how to display a meaningful message in case of any exception occurs.