【发布时间】:2018-12-10 16:00:09
【问题描述】:
这个主题来自我之前的问题:How to remove and create log in Windows Event Viewer
我创建了 wpf 应用程序。我通过 3 种方式捕获未处理的异常:
public partial class App : Application
{
public App()
{
DispatcherUnhandledException += App_DispatcherUnhandledException;
Dispatcher.UnhandledException += Dispatcher_UnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
}
private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
}
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
}
}
我正在创建这样的异常:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
throw new Exception("Test exception");
}
}
在执行方法(Dispatcher_UnhandledException,CurrentDomain_UnhandledException,App_DispatcherUnhandledException)之后,仍然抛出这个异常。并且 Windows 事件查看器正在创建这样的日志
描述:进程因未处理的异常而终止。 异常信息:System.InvalidOperationException 在 System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(System.Data.Common.DbConnection,System.Threading.Tasks.TaskCompletionSource
1<System.Data.ProviderBase.DbConnectionInternal>, System.Data.Common.DbConnectionOptions, System.Data.ProviderBase.DbConnectionInternal, System.Data.ProviderBase.DbConnectionInternal ByRef) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionFactory, System.Threading.Tasks.TaskCompletionSource1
【问题讨论】:
-
你的意思是当抛出异常时,这三个方法实际上都没有被调用?当您说“执行方法后 (...)”时,我不太确定您的意思。
-
您可能应该修复原始异常,或者至少记录它,而不是试图隐藏它。失败的数据库连接不会自行修复。
-
将事件参数的handled设置为true以停止事件传播。
标签: c# wpf unhandled-exception