30 September, 2010

WP7: Consistent error handling and how to display errors on a Windows Phone

As the UI of a phone is somewhat different than a traditional “interface” (i.e. a PC); you have to apply different approaches when it comes to displaying errors to the user.

In the “normal world” – e.g. a web application has a common error page that you lead your users to when something unfortunate happens. This is setup in web.config as a dedicated page for errors. On the Windows Phone, you can re-use that concept and have a common error page. Not that it is setup in app.config; but it is a common page you use for displaying “human readable” error messages to the user. The concept is seen here:

image

When an unhandled exception has happened; this is caught in the App (Application) and the Application is redirected to the error page (ErrorView). The below is taken from App.xaml.cs and presents the Application_UnhandledException method. This is by default hooked up in the constructor of the App-class.

// 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 into the debugger
System.Diagnostics.Debugger.Break();
}

//is handled by this method.
e.Handled = true;

//route to error view
Dictionary<string,string> data = new Dictionary<string,string>();
data.Add(Constants.ERROR_NAME, e.ExceptionObject.Message);

ApplicationController.Default.NavigateTo(ViewType.Error, data);
}

As seen, then an unhandled exception is caught in the App class, the Exception is set to Handled = true and the ApplicationController is used for navigating to the ErrorView. This will then be presented to the User as a common error page. What happens next, is the responsibility of the developer and your application context ;-)


image


 

Technorati Tags:

3 comments:

Anonymous said...

if you have custom error page how do you handle the back button?

e.g.
User opens app at page1, navigates to page2.
Page2 causes an exception and navigates to error page.
At this point the navigation stack has
Page1->Page2->Error
clicking back on the phone returns to page2 that then raises another exception, the user is trapped !

Claus Konrad said...

If your application fails just by opening "Page 2", you are having a problem of a different magnitude. A problem that needs serious addressing. You are correct; if that is the case - you will be thrown back to the error page immediately when pressing the back button on the phone.

Your application should at least be able to load the page. Do note that the errorpage presented on this blog was intended for "Unhandled Exceptions"; not ordinary exceptions that you as a developer should handle in the faulting page itself.

prob said...

ApplicationController , Constants,Viewtype "do not exist in the current context"
What should i do ? thx

iPhone/XCode - not all cases are equal!

This bit me! Having made some changes to an iPhone application (Obj-C); everything worked fine in the simulator. But, when deploying the s...