Handling Unhandled Exceptions in XBAP Applications

In your own applications you'll generally want a "catch-all" handler that will take care of unhandled exceptions. In WinForms apps this is done by creating an unhandled exception delegate and (optionally) creating an AppDomain unhandled exception handler. Peter Bromberg has a good article on all of this here and I wrote about the various options for WinForms apps here.

With XBAP (XAML Browser Applications) the rules are slightly different so here's one way to do it.

Take your existing XBAP app (or create a new one) and in the App.xaml.cs file you'll want to create a new event handler for unhandled exceptions. You can do this in the Startup method like so:

        protected override void OnStartup(StartupEventArgs e)


            DispatcherUnhandledException += App_DispatcherUnhandledException;



In our exception handler, we'll do two things. First we'll set the exception to be handled and then we'll set the content of the MainWindow (a property of the Application class) to be a new exception handler page.

        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)


            e.Handled = true;

            MainWindow.Content = new ExceptionHandlerPage();


That's really the basics and works. However you don't have the exception information passing onto the new page. We can do something simple for now. Here's the XAML for a simple error handling page:

<Page x:Class="XbapExceptionHandlerSpike.ExceptionHandlerPage"



    Width="300" Height="300"




            <RowDefinition Height="23" />

            <RowDefinition />


        <TextBlock Margin="10,0,0,0" VerticalAlignment="Center" Grid.Row="0" Text="An exception has occured. Here are the details:" />

        <TextBlock Margin="10,0,0,0" Grid.Row="1" x:Name="ErrorInformation" Foreground="Red" FontFamily="Consolas" TextWrapping="Wrap" />



I gave a name to the TextBlock in the second row in the grid. This is the control that will display our error message. I've also styled it and set the font to be a mono-spaced font.

We can update our creation of the ExceptionHandlerPage class to include the exception details like so:

        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)


            e.Handled = true;

            var page = new ExceptionHandlerPage


                               ErrorInformation = {Text = e.Exception.Message}


            MainWindow.Content = page;


Now our page displays the error with the details provided:

Again, this is really simple and bare-bones. You can get creative with it with things like fancy fonts, dancing bears, floating borders, etc. and passing along the entire Exception (so you might walk through the inner exceptions and details) and even log the exception to your bug tracking system. Jeff Atwood has a great article on creating friendly custom Exception Handlers (one for WinForms here, one for ASP.NET here). A WPF version might be useful.

As with WPF apps, there are a lot of ways to skin this cat. This is just my take on it. Feel free to offer your own twist.

1 Comment

Comments have been disabled for this content.