This documentation is archived and is not being maintained.

WindowsFormsApplicationBase.UnhandledException Event

Occurs when the application encounters an unhandled exception.

Namespace:  Microsoft.VisualBasic.ApplicationServices
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public event UnhandledExceptionEventHandler UnhandledException

An application raises the UnhandledException event when it encounters an unhandled exception. This event is part of the Visual Basic Application model. For more information, see Overview of the Visual Basic Application Model.

You can use the Exception property of the e parameter to access the unhandled exception that caused this event.

You can use the ExitApplication property of the e parameter to control whether the application exits. By default, ExitApplication is True. Therefore, the application exits after completing the UnhandledException event handler. You can set the value to False in the UnhandledException event handler to keep the application running, and have it return to a waiting state.

The code for the UnhandledException event handler is stored in the ApplicationEvents.vb file, which is hidden by default.

To access the Code Editor window for application events

  1. With a project selected in Solution Explorer, click Properties on the Project menu.

  2. Click the Application tab.

  3. Click the View Application Events button to open the Code Editor.

For more information, see How to: Handle Application Events (Visual Basic).


The Visual Basic compiler prevents applications that are built for debugging from raising this event, to enable a debugger to handle the unhandled exceptions. This means that if you are testing your application by running it under the Visual Studio Integrated Development Environment debugger, your UnhandledException event handler will not be called. For more information about building applications for debugging, see /debug (Visual Basic).


The following table lists examples of tasks that involve the My.Application.UnhandledException event.



Use the events provided by the Visual Basic Application Model to run code

How to: Run Code When the Application Starts or Ends (Visual Basic)

Log unhandled exceptions

How to: Log Exceptions in Visual Basic

Availability by Project Type

Project type


Windows Forms Application


Class Library


Console Application


Windows Forms Control Library


Web Control Library


Windows Service


Web Site


This example uses the My.Application.UnhandledException event to log any unhandled exceptions.

You must enter the code in the Code Editor window for application events. To access this window, follow the procedure found in this topic's Remarks section. For more information, see How to: Handle Application Events (Visual Basic).

Because the UnhandledException event is not raised when a debugger is attached to the application, you have to run this example outside the Visual Studio Integrated Development Environment.

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

        "Unhandled Exception.")
End Sub

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.