WPF Application Wide Exception Handling

What do you do if you want to catch all unhandled exceptions in an application?

In App.xaml:

<Application x:Class="MyApp.App"

Add "DispatcherUnhandledException" to App.xaml. Then in your App.xaml.cs file:

private void UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) {
    var time = DateTime.Now;
    File.AppendAllText(@"C:\exps.csv", string.Format("{0},{1}", time, e.Exception.Message));
    File.AppendAllText(@"C:\sts.txt", time + ":\n" + e.Exception.StackTrace + "\n");
    e.Handled = false; //we want the user to experience the crash

That's it. So now if the app crashes on your users, you don't have to ask your users what steps they did to cause the crash. You can just look at the logs!

If you made it this far, you should follow me on Twitter.


Want to test-drive Bitcoin without any risk? Check out my bitcoin wallet Coinbolt. It includes test coins for free.

comments powered by Disqus