Async Void, Exceptions and Windows 8.1

Many people including myself have talked about the dangers of async void methods in your Windows 8 application. The fact that exceptions in these methods ultimately caused the application to crash was a big problem, realistically though trying to build a proper application with no async void methods is an exercise in futility. There are ways to mitigate the problem, from replacing the Synchronization Context like we do at Marker Metro, IL Weaving using Fody to plain exception handlers in every async void method.

Thankfully in appears that in Windows 8.1 some of these problems have been alleviated, you’ll notice now that exceptions throw in async void methods will now be passed to the Application.UnhandledException event. There if you set the event arguments Handled property to true you can recover your application from perhaps an unnecessary crash.

protected override void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
    e.Handled = true;

All in all I love these little updates to WinRT, here’s hoping we can find some more of them.