处理异常后发生未处理的异常
Unhandled exception occurs after handling exception
这个主题来自我的 preveus 问题:
我创建了 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");
}
}
执行完method(Dispatcher_UnhandledException
,CurrentDomain_UnhandledException
,App_DispatcherUnhandledException
)后依然抛出异常。 Windows 事件查看器正在创建这样的日志
Description: The process was terminated due to an unhandled exception.
Exception Info: System.InvalidOperationException
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(System.Data.Common.DbConnection, System.Threading.Tasks.TaskCompletionSource1<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.TaskCompletionSource
1
在您的处理程序方法中,您需要告诉 .NET 您处理了异常。否则它仍然会杀死应用程序。您可以使用 Handled
property of the DispatcherUnhandledExceptionEventArgs
对象来执行此操作。
因此,如果您决定尽管出现异常仍要继续申请,请将其设置为 true
:
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
e.Handled = true; //let's continue and see if anything explodes
}
也就是说,您仍然应该处理可能发生的异常(尽可能多)。就拿你举的例子来说,貌似是网络连接问题。如果您在创建连接时捕获了该异常,则可以告诉用户更具体的内容,例如 "I could not connect to the database. Is your network working?" 而如果您依靠它来捕获此类错误,则只能反省异常消息,而这通常不会对用户有意义。
这应该用作最后的故障安全手段。它不应取代整个代码中的捕获异常。
这样的全局错误处理应该会关闭应用程序。
您从某处或其他地方遇到异常,并且该应用程序处于某种未知状态。
如果你吃掉错误并让用户继续,那么你将无法知道会发生什么。
"standard" 方法是记录错误然后使用
关闭
Application.Current.Shutdown();
这个主题来自我的 preveus 问题:
我创建了 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");
}
}
执行完method(Dispatcher_UnhandledException
,CurrentDomain_UnhandledException
,App_DispatcherUnhandledException
)后依然抛出异常。 Windows 事件查看器正在创建这样的日志
Description: The process was terminated due to an unhandled exception. Exception Info: System.InvalidOperationException at 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.TaskCompletionSource
1
在您的处理程序方法中,您需要告诉 .NET 您处理了异常。否则它仍然会杀死应用程序。您可以使用 Handled
property of the DispatcherUnhandledExceptionEventArgs
对象来执行此操作。
因此,如果您决定尽管出现异常仍要继续申请,请将其设置为 true
:
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
e.Handled = true; //let's continue and see if anything explodes
}
也就是说,您仍然应该处理可能发生的异常(尽可能多)。就拿你举的例子来说,貌似是网络连接问题。如果您在创建连接时捕获了该异常,则可以告诉用户更具体的内容,例如 "I could not connect to the database. Is your network working?" 而如果您依靠它来捕获此类错误,则只能反省异常消息,而这通常不会对用户有意义。
这应该用作最后的故障安全手段。它不应取代整个代码中的捕获异常。
这样的全局错误处理应该会关闭应用程序。 您从某处或其他地方遇到异常,并且该应用程序处于某种未知状态。
如果你吃掉错误并让用户继续,那么你将无法知道会发生什么。
"standard" 方法是记录错误然后使用
关闭 Application.Current.Shutdown();