<Application
x:Class="DispatcherUnhandledExceptionSample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
DispatcherUnhandledException="App_DispatcherUnhandledException" />
//File:Window.xaml.cs
using System;
using System.Diagnostics;
using System.Windows;
using System.Data;
using System.Xml;
using System.Configuration;
using System.Windows.Threading;
namespace DispatcherUnhandledExceptionSample
{
public partial class App : Application
{
void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
bool shutdown = false;
if (e.Exception is DivideByZeroException)
{
shutdown = false;
}
else if (e.Exception is ArgumentNullException)
{
shutdown = true;
}
if (shutdown)
{
MessageBoxResult result = MessageBox.Show("Application must exit:\n\n" + e.Exception.Message + "\n\nSave before exit?", "app", MessageBoxButton.YesNo, MessageBoxImage.Error);
if (result == MessageBoxResult.Yes)
{
}
EventLog.WriteEntry("app", "Unrecoverable Exception: " + e.Exception.Message, EventLogEntryType.Error);
this.Shutdown(-1);
}
e.Handled = true;
}
}
}