Monday, May 17, 2010

Dealing with Unhandled exceptions in WPF

In you App.Xaml.cs file put the following:

private void App_DispatcherUnhandledException(object sender,
System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show("An unhandled " + e.Exception.GetType().ToString() +
" exception was caught and ignored.");
e.Handled = true;
}


Add the following to the App.xaml file

DispatcherUnhandledException="App_DispatcherUnhandledException"

to the Application tag of App.xaml like this:
<Application x:Class="Something.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml" Startup="Application_Startup"
DispatcherUnhandledException="App_DispatcherUnhandledException">

No comments: