WindowsFormsApplicationBase.UnhandledException 事件

定義

發生於應用程式遇到未處理的例外狀況。

public:
 event Microsoft::VisualBasic::ApplicationServices::UnhandledExceptionEventHandler ^ UnhandledException;
public event Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventHandler UnhandledException;
member this.UnhandledException : Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventHandler 
Public Custom Event UnhandledException As UnhandledExceptionEventHandler 

事件類型

範例

此範例會 My.Application.UnhandledException 使用 事件來記錄任何未處理的例外狀況。

您必須在應用程式事件的 [程式代碼 編輯器] 視窗中輸入程式代碼。 若要存取此視窗,請遵循本主題的一節中找到的程式。 如需詳細資訊,請參閱 Application Page, Project Designer (Visual Basic)

UnhandledException由於調試程式附加至應用程式時不會引發事件,因此您必須在Visual Studio集成開發環境之外執行這個範例。

Private Sub MyApplication_UnhandledException(
    sender As Object,
    e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs
) Handles Me.UnhandledException

    My.Application.Log.WriteException(e.Exception,
        TraceEventType.Critical,
        "Unhandled Exception.")
End Sub

備註

當應用程式遇到未處理的例外狀況時, UnhandledException 就會引發 事件。 此事件是 Visual Basic 應用程式模型的一部分。 如需詳細資訊,請參閱 Visual Basic應用程式模型概觀

您可以使用 Exception 參數的 e 屬性來存取造成此事件之未處理的例外狀況。

您可以使用 ExitApplication 參數的 e 屬性來控制應用程式是否結束。 根據預設, ExitApplicationTrue。 因此,應用程式會在完成 UnhandledException 事件處理程序之後結束。 您可以在事件處理程式中UnhandledException將 值False設定為 ,讓應用程式保持執行狀態,並讓它返回等候狀態。

事件處理程式的程式 UnhandledException 代碼會儲存在預設隱藏的 ApplicationEvents.vb 檔案中。

存取應用程式事件的 [程序代碼 編輯器] 視窗
1.在 方案總管 中選取專案時,單擊 [專案] 功能表上的 [屬性]。
2. 按一下 [應用程式]索引標籤。
3.按兩下 [檢視應用程式事件] 按鈕,以開啟程式代碼 編輯器。

如需詳細資訊,請參閱 Application Page, Project Designer (Visual Basic)

注意

Visual Basic 編譯程式會防止建置偵錯的應用程式引發此事件,讓調試程式能夠處理未處理的例外狀況。 這表示如果您在 Visual Studio 集成開發環境調試程式下執行應用程式來測試應用程式, UnhandledException 則不會呼叫事件處理程式。 如需建置應用程式以進行偵錯的詳細資訊,請參閱 /debug (Visual Basic)

工作

下表列出涉及 My.Application.UnhandledException 事件的工作範例。

收件者 請參閱
記錄未處理的例外狀況 作法:記錄例外狀況

依專案類型的可用性

專案類型 可用
Windows Forms 應用程式
類別庫
主控台應用程式
Windows Form 控制項程式庫
Web 控制項程式庫
Windows 服務
網站

適用於

另請參閱