wpf 未处理的异常在被捕获后仍然使应用程序崩溃

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12024470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:01:22  来源:igfitidea点击:

Unhandled Exception Still Crashes Application After Being Caught

wpfvisual-studio-2010c#-4.0exception-handlingunhandled-exception

提问by Landin Martens

I have a WPF application that consists of multiple projects that have forms, classes, base classes, etc..

我有一个 WPF 应用程序,它由多个具有表单、类、基类等的项目组成。

Because of the large code base I want to make sure if an exception does happen I can catch it, notify the user and let the application continue without crashing. I understand the pros and cons to doing this.

由于代码库很大,我想确保是否确实发生了异常,我可以捕获它,通知用户并让应用程序继续运行而不会崩溃。我理解这样做的利弊。

In the App.xaml.cs of the application I have:

在应用程序的 App.xaml.cs 中,我有:

private void OnApplicationStartup(object sender, StartupEventArgs e)
{
    Application.Current.DispatcherUnhandledException += CurrentOnDispatcherUnhandledException;
    AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
    Dispatcher.UnhandledException += DispatcherOnUnhandledException;
    UI.Views.Windows.MainWindow.Show();
}

private void DispatcherOnUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs dispatcherUnhandledExceptionEventArgs)
{
    MessageBox.Show("TEST 3");
}

private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
    MessageBox.Show("TEST 2");
}

private void CurrentOnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs dispatcherUnhandledExceptionEventArgs)
{
    MessageBox.Show("TEST 1");
}

If an exception happens anywhere those messages boxes are shown which is great, the problem is right after it shows the message the application still crashes. If I run the application in debug, Visual Studio will jump to the place were the exception happened and if I continue it will then go to the message box.

如果在显示这些消息框的任何地方发生异常,这很好,那么在显示应用程序仍然崩溃的消息之后,问题就出现了。如果我在调试中运行应用程序,Visual Studio 将跳转到发生异常的位置,如果我继续,它将转到消息框。

I think the problem has something to do with that but I am not sure. Is there a way to catch the exception like I am above but at the same time not have the application crash after?

我认为问题与此有关,但我不确定。有没有办法像我上面那样捕获异常,但同时没有应用程序崩溃?

Thank You

谢谢你

EDITThe exceptions that get through to the UnhandledException section will be things like NullReference, NotSupported or Database Exceptions for the most park. If a "serious" exception gets cought like Stack Overflow I will simple notify the user and kill the app. I still need to find a way to stop the app from crashing on non serious exceptions though.

编辑进入 UnhandledException 部分的异常将是大多数公园的 NullReference、NotSupported 或 Database Exceptions 之类的东西。如果像 Stack Overflow 一样出现“严重”异常,我会简单地通知用户并终止应用程序。我仍然需要找到一种方法来阻止应用程序在非严重异常时崩溃。

回答by Ivo

I think you need to set

我认为你需要设置

dispatcherUnhandledExceptionEventArgs.Handled = true;

回答by rizalp1

What kind of exception is your application catching? Using UnhandledException for exceptions that change state will not work unless you are setting HandleProcessCorruptedStateExceptionsAttribute attribute.

您的应用程序捕获了什么样的异常?除非您设置 HandleProcessCorruptedStateExceptionsAttribute 属性,否则将 UnhandledException 用于更改状态的异常将不起作用。

From MSDN documentation:
AppDomain.UnhandledException Event

Starting with the .NET Framework 4, this event is not raised for exceptions that corrupt the state of the process, such as stack overflows or access violations, unless the event handler is security-critical and has the HandleProcessCorruptedStateExceptionsAttribute attribute.

从 .NET Framework 4 开始,对于破坏进程状态的异常(例如堆栈溢出或访问冲突),不会引发此事件,除非事件处理程序对安全至关重要并且具有 HandleProcessCorruptedStateExceptionsAttribute 属性。

http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx

http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx