我试图在我的 WPF 应用程序中捕获所有异常。我尝试了以下代码但它不起作用我不知道为什么?
<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>
应用程序.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);
}
稍后,我会将所有异常写入日志表。
相关分类