gpt4 book ai didi

c# - Visual Studio 2008 - 发生未处理的异常时应用程序关闭

转载 作者:太空宇宙 更新时间:2023-11-03 11:56:36 25 4
gpt4 key购买 nike

当调试 WinForm VB.NET 项目时发生未处理的异常时,我遇到了问题。

问题是我的应用程序终止了,我必须重新启动应用程序,而不是像在 VS2003 中那样重试该操作

未处理的异常在 ApplicationEvents.vb 中的新 My.MyApplication 类中实现

Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
Dim handler As New GlobalErrorHandler()
handler.HandleError(e.Exception)

e.ExitApplication = False
End Sub

注意:handler.HandleError 仅显示一个对话框并将错误记录到日志文件中。

我还尝试了以下用于 VS2003 的代码,但在 VS2008 中运行时会导致相同的行为:

    AddHandler System.Windows.Forms.Application.ThreadException, AddressOf OnApplicationErrorHandler
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf OnUnhandledExceptionHandler

OnApplicationErrorHandler 和 OnUnhandledExceptionHandler 与 handle.HandleError 的作用相同

在 VS2008 之外运行应用程序会产生预期的行为(应用程序不会终止),但它会增加调试期间的测试周期。

更新:我在答案中添加了示例代码以在 C# 中演示此问题

最佳答案

好的,我已经在这方面做了更多的工作,但仍然没有答案。我发现这个问题不仅与VB.NET有关,而且与C#有关

这是演示问题的简单 C# 程序:

using System;
using System.Windows.Forms;
namespace TestCSharpUnhandledException
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}

void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show("Oops an unhandled exception, terminating:" + e.IsTerminating);
}

void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
MessageBox.Show("Oops an unhandled thread exception");
}

private void button1_Click(object sender, EventArgs e)
{
throw new Exception("Dummy unhandled exception");
}
}
}

我发现有趣的是,在 VS 中调试会引发 UnhandledException 事件,但在 VS 外部运行应用程序会导致 ThreadException 触发。

同样在 VS 内部,e.IsTerminating 设置为 true,最终我希望它为 false。 (这是一个只读属性)

关于c# - Visual Studio 2008 - 发生未处理的异常时应用程序关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/188098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com