I was recently looking at the latest Windows Phone 8 questions over at Stack Overflow and I saw this one. Avinash was asking how to save an explicit version of the crash dump on his Windows Phone 8.1 WinRT app.

I haven’t converted my apps to WinRT or played with WinRT apps yet, but I decided to create a test project to see if I could answer his question. A simple Google search didn’t return any good results so after I figured it out, I decided to write this post.

This only applies to Windows Phone 8.1 projects that use Windows Runtime. You do not have to do this if you are creating a Silverlight 8.1 app as it is already done for you.

What happened to Application_UnhandledException?

For some reason, when you create a new Windows Phone 8.1 project that uses the Windows Runtime (WinRT), inside App.xaml.cs you won’t find the Application_UnhandledException event handler that exists in the Silverlight 8.0 and 8.1 project templates.

In WinRT, you have to write the event handler method and subscribe to it manually.

Solution

Open App.xaml.cs and add the following line in the constructor:

this.UnhandledException += Application_UnhandledException;

 

Now add the event handler method:

void Application_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
	// You can process the unhandled exception here
}

 

Now inside the event handler you have access to the unhandled exception and you can save a log if you want. If you are looking for a good and quick Crash Report solution for Windows Phone apps which lets the users email the crash report, you should check this post by Simon (look at the Silverlight section).

Hopefully this clears the confusion on how to access the Unhandled Exceptions in WinRT templates.