MOBILE

Windows Phone 7 Development : Debugging Application Exceptions (part 1) - Debugging Page Load Exceptions

1/19/2011 11:34:11 AM
In this section, you will learn how to find and deal with two exceptions that are common to Windows Phone applications. The first is the navigation failed exception, which can be thrown when a main page is loaded; the second deals with the web service call that is consumed by the application.

The ErrorHandlingDemo application that you'll use contains two projects, a Windows Phone project and the Calculator web service project, which has an Add method that adds two numbers and returns the result to the caller. Being able to debug and handle web service call exceptions will be critical especially if you are planning to work with external services like Microsoft Bing Maps services.

When you finally debug and fix all the issues in the application, you will see the result shown in Figure 1.

Figure 1. ErrorHandlingDemo application

1. Debugging Page Load Exceptions

TheErrorHandlingDemo application contains bugs that will cause exceptions to be thrown when the application's main page is loaded. In this section, you will learn how to find and fix such problems in Visual Studio.

1.1. Catching an Exception

Whenever an application throws an exception, Visual Studio will stop execution at the line where it's thrown. To observe this behavior, let's run the application and take a closer look using the Visual Studio IDE.

Fire up Visual Studio, select File=>Open, and browse to the following file, which you can download from the site for this book:

{unzippeddirectory}\ch04\ErrorHandlingDemo\Start\ErrorHandlingDemo.sln.

Once the solution file is loaded, press F5 to run it.

Notice the raised exception message in Figure 2, which points to the code that has caused ArgumentOutOfRangeExceptionto be thrown. From DeviceExtendedProperties you can obtain Windows Phone 7 system information, and only the following keys—DeviceManufacturer, DeviceName, DeviceUniqueId, DeviceTotalMemory, ApplicationCurrentMemoryUsage, andApplicationPeakMemoryUsage—will be available. And when you try to query the system information DName that does not exist, the application throws ArgumentOutOfException.

Figure 2. A raised exception in Visual Studio

1.2. Querying Exception Object Values with Immediate Window

Whenever an application running in Visual Studio pauses at a line where an exception has been thrown, you always have an opportunity to observe its variables in Visual Studio's Immediate Window.

Immediate Window is a most useful debugging feature because it allows you to evaluate any statement when the code execution pauses at the breakpoint. If you do not see the immediate window when the breakpoint is hit, you can go to Debug => Windows => Immediate to bring up the Immediate Window, as shown in Figure 4-3.

  1. With ErrorHandlingDemo still paused in the debugger, go to the Immediate Window, type in DeviceExtendedProperties.GetValue("DeviceName") to query the object value, and press Enter. You will see the result printed in an Immediate Window, as shown in Figure 3.

    Figure 3. Query object value in Immediate Window
1.3. Catching an Unhandled Exception in RootFrame_NavigationFailed or Application_UnhandledException

Unhandled exceptions in a Windows Phone application will be caught by one of two main methods: RootFrame_NavigationFailed, andApplication_UnhandledException.RootFrame_NavigationFailed catches unhandled exceptions thrown while a page is being loaded; Application_UnhandledException catches exceptions thrown in all other cases.

  1. Press F5 to continue debugging from the breakpoint in the previous section.

The debugger will next break inside RootFrame_NavigationFailed in App.xaml.cs as shown in Figure 4. Notice that in App.xaml.cs you will find various Windows Phone application–related events such as Application_Launching, Application_Activated, Application_Deactivated, Application_Closing, RootFrame_NavigationFailed, andApplication_UnhandledException. As far as exceptions are concerned, only two events will be of interest. RootFrame_NavigationFailed captures unhandled exceptions when the Windows Phone page fails to load. In ErrorHandlingDemo, unhandled exceptions occur when MainPage tries to load and throws ArgumentOutOfException.

Figure 4. Breakpoint at RootFrame_NavigationFailed

  1. With your mouse, hover over NavigationFailedEventArgs e and you will be able to drill into the object value and see the e.Uri that contains the page that caused the error during the load, as shown in Figure 5.

    Figure 5. NavigationFailedEventArgs.Uri
  2. Press F5 to continue debugging, and you will notice that code execution next breaks in the Application_UnhandledException method. All exceptions that are not handled specifically by a try-catch-finally block will ultimately end up in this method.

1.4. Handling an Exception RootFrame_NavigationFailed

When an exception is thrown in the MainPage of an application, the exception will be caught by the RootFrame_NavigationFailed method, and this is where you want to handle it in order to stop the exception from bubbling up to the Application_UnhandledException method.

In ErrorHandlingDemo, replace the RootFrame_NavigationFailed method with following code. Notice the use of MessageBox in the code to display the proper error with stack trace and set e.Handled to true, which will stop the breakpoint to move to the Application_UnhandledException method.

// Code to execute if a navigation fails
private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached)
{
// A navigation has failed; break into the debugger
System.Diagnostics.Debugger.Break();
}

MessageBox.Show(
string.Format("Page {0} failed to load because of with error: {1}",
e.Uri.ToString(), e.Exception.StackTrace));
e.Handled = true;
}

1.5. Fixing the Error in the Code

In the previous section, you added a MessageBox display in case any other page fails to load, and in the following steps, you will be fixing the actual cause of the exception in MainPage. But first, let's fix the error in MainPage.xaml.cs.

Fix the error in MainPage.xaml.cs by replacing

txtDeviceName.Text = DeviceExtendedProperties.GetValue("DName").ToString()
with
txtDeviceName.Text = DeviceExtendedProperties.GetValue("DeviceName").ToString().

Other  
  •  Programming the Mobile Web : JavaScript Libraries
  •  Programming the Mobile Web : Ajax Support
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 5) - Deploying the Service to Windows Azure
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 4) - Coding NotepadViewModel
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 3) - Coding the BoolToVisibilityConvert
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 2) - Coding MainPage
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 1) - Building the User Interface
  •  Building Android Apps : Detecting Browsers with WURFL
  •  Building Android Apps : Submitting Your App to the Android Market - Preparing a Release Version of Your App
  •  Windows Phone 7 Development : Creating a Cloud Service to Access the Cloud Database (part 2) - Implementing a WCF Service to Access the SQL Azure Database
  •  
    Video
    Video tutorials
    - How To Install Windows 8

    - How To Install Windows Server 2012

    - How To Install Windows Server 2012 On VirtualBox

    - How To Disable Windows 8 Metro UI

    - How To Install Windows Store Apps From Windows 8 Classic Desktop

    - How To Disable Windows Update in Windows 8

    - How To Disable Windows 8 Metro UI

    - How To Add Widgets To Windows 8 Lock Screen

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010
    programming4us programming4us
    Top 10
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    Popular Tags
    Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone