我试图在我的 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.csprotected 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); }稍后,我会将所有异常写入日志表。
1 回答
- 1 回答
- 0 关注
- 161 浏览
添加回答
举报
0/150
提交
取消