My.Application.UnhandledException EventĀ
Occurs when the application encounters an unhandled exception.
' Usage
Public Sub Me_UnhandledException( _
ByVal sender As Object, _
ByVal e As UnhandledExceptionEventArgs _
) Handles Me.UnhandledException
End Sub
' Declaration
Public Event UnhandledException( _
ByVal sender As Object, _
ByVal e As UnhandledExceptionEventArgs _
)
Parameters
- sender
The Object that raised the event.
- e
An UnhandledExceptionEventArgs object that contains the unhandled exception and additional information.
Remarks
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, so the application exits after completing the UnhandledException event handler. You can set the value to True 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:
With a project selected in Solution Explorer, click Properties on the Project menu.
Click the Application tab.
Click the View Application Events button to open the Code Editor.
For more information, see How to: Handle Application Events (Visual Basic).
Note
The Visual Basic compiler prevents applications that are built for debugging from raising this event, to allow 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 on building applications for debugging, see /debug (Visual Basic).
Tasks
The following table lists examples of tasks involving the My.Application.UnhandledException event.
To | See |
---|---|
Use the events provided by the Visual Basic Application Model to run code |
|
Log unhandled exceptions |
Example
This example uses the My.Application.UnhandledException event to log any unhandled exceptions.
Private Sub MyApplication_UnhandledException( _
ByVal sender As Object, _
ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs _
) Handles Me.UnhandledException
My.Application.Log.WriteException(e.Exception, _
TraceEventType.Critical, _
"Unhandled Exception.")
End Sub
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 need to run this example outside of the Visual Studio Integrated Development Environment,
Requirements
Namespace: Microsoft.VisualBasic.ApplicationServices
Class: WindowsFormsApplicationBase
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
Availability by Project Type
Project type | Available |
---|---|
Windows Application |
Yes |
Class Library |
No |
Console Application |
No |
Windows Control Library |
No |
Web Control Library |
No |
Windows Service |
No |
Web Site |
No |
Permissions
The following permissions may be necessary:
Permission | Description |
---|---|
Controls the ability to add an event handler for this event. Associated enumeration: System.Security.Permissions.SecurityPermissionFlag.ControlAppDomain. |
For more information, see Code Access Security and Requesting Permissions.
See Also
Reference
My.Application Object
Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs