WPF: Exception handling suggestions

XAML and Exception Handling is quite a tricky combination. And while I understand that it hard to design a robust and serving way of catching errors in XAML world it always feels a bit like way back when we had no exceptions; just breaking applications.

 

In general my WPF apps break down on the Application.Run() with, when I’m lucky, some vague exception deep down the list of inner exceptions.

 

It logical: there’s no user code running, in fact, all we program in XAML is object state and the moment it gets loaded into memory it is the WPF runtime that’s running the code. Nothing to debug, catch or handle there that is specific to my code.

 

In this post[1] Karsten explains how to handle exception in a WPF application. It feels like programming the global.asax in ASP.NET to do exceptionhandling.

 

My suggestion to Microsoft is (are you listening?): Provide an extra event for each(?) WPF element so I can code like this:

 

&nbsp;&nbsp; 1:&nbsp;&nbsp;<Window&nbsp;x:Class=”_1_Hello.Window1″

&nbsp;&nbsp;&nbsp;2:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;>=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

&nbsp;&nbsp;&nbsp;3:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;>=”http://schemas.microsoft.com/winfx/2006/xaml”

&nbsp;&nbsp;&nbsp;4:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Title=”_1_Hello” HandleException=”Window1_HandleException”

&nbsp;&nbsp;&nbsp;5:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;>

&nbsp;&nbsp;&nbsp;6:&nbsp;&nbsp;&nbsp;&nbsp;<Grid&nbsp;VerticalAlignment=”Center”&nbsp;HorizontalAlignment=”Center”>

&nbsp;&nbsp;&nbsp;7:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Grid&nbsp;ShowGridLines=”True” HandleException=”InnerGrid_HandleException”>

&nbsp;&nbsp;&nbsp;8:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Grid.RowDefinitions>

&nbsp;&nbsp;&nbsp;9:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<RowDefinition>

&nbsp;&nbsp;10:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</RowDefinition>

&nbsp;&nbsp;11:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<RowDefinition>

&nbsp;&nbsp;12:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</RowDefinition>

&nbsp;&nbsp;13:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Grid.RowDefinitions>

&nbsp;&nbsp;14:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Grid.ColumnDefinitions>

&nbsp;&nbsp;15:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<ColumnDefinition></ColumnDefinition>

&nbsp;&nbsp;16:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<ColumnDefinition></ColumnDefinition>

&nbsp;&nbsp;17:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Grid.ColumnDefinitions>

&nbsp;&nbsp;18:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Label&nbsp;Grid.Column=”0″&nbsp;Grid.Row=”0″>Hello&nbsp;World</Label>

&nbsp;&nbsp;19:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Button&nbsp;Grid.Column=”1″&nbsp;Grid.Row=”1″&nbsp;Click=”OkButtonClick”>Ok</Button>

&nbsp;&nbsp;20:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Grid>

&nbsp;&nbsp;21:&nbsp;&nbsp;&nbsp;&nbsp;</Grid>

&nbsp;&nbsp;22:&nbsp;&nbsp;</Window>

&nbsp;

This way exceptions can be bubbled up or handled on a lower level in context.

&nbsp;