MOBILE

Windows Phone 7 Development : Handling Errors in Rx.NET

4/23/2011 3:56:55 PM
In the world of asynchronous programming, and especially in the world of distributed asynchronous programming, errors are a fact of life and should be expected. Rx.NET Observers provide a separate OnError event handler to deal with unforeseen errors that may arise. For instance, to make the WeatherRx application more robust, let's add an OnError handler to the weather.Subscribe call. The resulting code would look like this:
weather.ObserveOn(Deployment.Current.Dispatcher).Subscribe(evt =>
{
if (evt.EventArgs.Result.Details != null)
{
lblWeatherFahrenheit.Text = "Current Weather, Fahrenheit: " +
evt.EventArgs.Result.Details[0].MinTemperatureF.ToString() + " - " +
evt.EventArgs.Result.Details[0].MaxTemperatureF.ToString();

lblCelsius.Text = "Current Weather, Celsius: " +
evt.EventArgs.Result.Details[0].MinTemperatureC.ToString() + " - " +
evt.EventArgs.Result.Details[0].MaxTemperatureC.ToString();



imgWeather.Source = new BitmapImage(new Uri(evt.EventArgs.Result.Details[0].WeatherImage,
UriKind.Absolute));
}
},
ex => {
Deployment.Current.Dispatcher.BeginInvoke(() => lblStatus.Text = ex.Message);
}
);


Note the somewhat cryptic (it's a lamda expression and it uses a lambda expression within its own body) use of the Deployment.Current.Dispatcher.BeginInvokeOnError statement to get around cross-thread access issues discussed previously. In the preceding code, the handler simply displays the exception text, but there is nothing stopping you from dissecting an error thoroughly and providing a possible corrective action. For instance, if the web service is not available at the address specified, you may retry your call to a different location of the web service. Rx.NET also has exception handling operators Catch, Finally, OnErrorResumeNext, and Retry, which aid in recovering from errors. You will explore some of those in the next section as we discuss some potential ways of handling intermittently available data connections on the phones.

Other  
  •  Windows Phone 7 Development : Using Rx.NET with Web Services to Asynchronously Retrieve Weather Data
  •  Windows Phone 7 Development : Media - Adding Sounds to an Application
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 4) - Implementing the Summary View
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 3) - Implementing the Volume View
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 2) - Implementing the Area View
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 1)
  •  Windows Phone 7 Development : Working with Video (part 2) - Coding the Application
  •  Windows Phone 7 Development : Working with Video (part 1)
  •  Windows Phone 7 Development : Plotting an Address on a Bing Maps Map and Working with the Bing Maps Service
  •  Windows Phone 7 Development : Using GeoCoordinateWatcher and the Bing Maps Control to Track Your Movements
  •  iPhone Application Development : Creating a Multi-View Toolbar Application (part 3) - Adding Toolbar Controls
  •  iPhone Application Development : Creating a Multi-View Toolbar Application (part 2) - Instantiating the View Controllers
  •  iPhone Application Development : Creating a Multi-View Toolbar Application (part 1)
  •  Windows Phone 7 Development : Using Location Services - Simulating the Location Service
  •  Introducing the Windows Phone Location Service and Mapping APIs
  •  iPhone Application Development : Implementing a Custom Picker View (part 4) - Tweaking the Picker UI
  •  iPhone Application Development : Implementing a Custom Picker View (part 3) - Reacting to a Picker View Choice
  •  iPhone Application Development : Implementing a Custom Picker View (part 2)
  •  iPhone Application Development : Implementing a Custom Picker View (part 1)
  •  Windows Phone 7 Development : Isolated Storage - Working with Isolated Storage Settings
  •  
    Top 10
    -  Monitoring a SharePoint 2010 Environment : Understanding Timer Jobs for SharePoint 2010
    -  Designing and Configuring Unified Messaging in Exchange Server 2010 : Unified Messaging Architecture (part 2)
    -  Mobile Application Security : Windows Mobile Security - Development and Security Testing (part 2)
    -  IIS 7.0 : Runtime Web Server Extensibility (part 1)
    -  Manipulate File Paths
    -  Architecting Applications for the Enterprise : UML Diagrams (part 1) - Use-Case Diagrams
    -  Create Your Own E-Books (Part 2) - Creation Services
    -  100 Windows Speed-Up Tips (Part 7) - Five ways Jump Lists can save your time
    -  Active Directory Domain Services 2008 : Automatically Populate a Migration Table from a Group Policy Object
    -  Understanding IIS 7.0 Architecture : IIS 7.0 Core Components
    Most View
    -  The Art of SEO : How Links Influence Search Engine Rankings (part 2) - Additional Factors That Influence Link Value
    -  Production Diagnostics Improvements in CLR 4
    -  Using Shapes in XAML
    -  Finite Automata and Regular Expressions
    -  Windows System Programming : The Registry
    -  Windows Server 2003 : Securing and Troubleshooting Authentication
    -  Windows Server 2008 : Transport-Level Security - Active Directory Rights Management Services
    -  Complete Guide for Your PC & How to Buy It (Part 3) - Lenovo ThinkPad Edge E520 & Toshiba Portégé Z830-104
    -  Windows Vista : Automating Recurrent Tasks (part 1) - Working with the Command Prompt
    -  Upgrading to Windows Server 2003 : Architectural Changes Since Windows NT 4.0
    -  System Center Configuration Manager 2007 : Developing the Solution Architecture (part 1) - Developing the Network Infrastructure
    -  BizTalk 2006 : Managing Exceptions in Orchestrations (part 3) - Running the EAIProcess
    -  The Rumour Mill – Dirty Laundry
    -  Is It Time To Ditch Windows Search? (Part 3) - Search across the LAN
    -  Understanding Active Directory Certificate Services (AD CS) in Windows Server 2008 R2
    -  Programming Microsoft SQL Server 2005 : An Overview of SQL CLR - CLR Triggers
    -  Mobile Application Security : SymbianOS Security - Persistent Data Storage
    -  Gamepad - How It Works (Part 1) - Arcade fever
    -  LG IPS235V : Full HD monitor with an IPS panel
    -  Programming the Mobile Web : Testing and Debugging (part 1) - Remote Labs