![](/img/trans.png)
[英]Visual Studio watch variable values when an unhandled exception occurs
[英]Visual Studio 2008 - Application closes when unhandled exception occurs
我在調試 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。 (這是只讀屬性)
好的,我在這篇博文中找到了這個問題的答案: Handling "Unhandled Exceptions" in .NET 2.0
謝謝馬克!
簡短的回答是:
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
這會在調試和運行模式期間導致相同的行為。 導致 VS 在調試時沒有關閉
我不確定 VS2008,但我在 VS2005 中遇到了同樣的問題。 事實證明,我只需要轉到 Debug->Exceptions(或 Crtl + Alt + E)並確保未選中所有拋出框,但選中所有用戶未處理的框。
你的代碼可能會有一些不同和時髦的東西,但它可能會解決這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.