I'm trying to catch all exceptions in my WPF App. I tried the following code but it is not working I don't know why?
<Application x:Class="DBFilter.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
Exit="Application_Exit"
DispatcherUnhandledException ="AppDispatcherUnhandledException"
>
<Application.Resources>
</Application.Resources>
</Application>
App.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(AppDomainUnhandledExceptionHandler);
System.Windows.Forms.Application.ThreadException += new
ThreadExceptionEventHandler(Application_ThreadException);
Application.Current.DispatcherUnhandledException += new
DispatcherUnhandledExceptionEventHandler(AppDispatcherUnhandledException);
}
void AppDomainUnhandledExceptionHandler(object sender,
UnhandledExceptionEventArgs ex)
{
Exception ex = (Exception)ea.ExceptionObject;
MessageBox.Show(ex.Exception.InnerException.Message);
}
void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.InnerException.Message);
}
void AppDispatcherUnhandledException(object
sender,DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.InnerException.Message);
}
Later, I will write all exceptions to a log table.