似乎未处理的异常只会在UI线程中崩溃程序。其他线程中的异常会无声地失败。我希望能够在这些线程中抛出异常时退出应用程序或显示有关异常的信息。不幸的是,UnhandledException
事件似乎只适用于一个线程。
private void Window_Loaded(object sender, RoutedEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Task.Run(() => throw new Exception("This is an exception."));
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show(e.ExceptionObject.ToString());
}
这既没有崩溃应用程序也没有通知我异常。有没有办法确保所有线程中的异常永远不会导致静默失败,无论是使用代码还是使用某种应用程序设置?
What is the best way to catch exception in Task?没有解决使用全局异常处理来防止静默失败的问题,因此它与我的问题非常不同。