You want to be notified if your application is about to exit because of an unhandled error, possibly so that you can log the problem or perform some final cleanup.
Create an event handler for the AppDomain.UnhandledException event.
The AppDomain.UnhandledException event fires when an unhandled error occurs, just before the application is terminated. This event doesn’t give you the chance to rectify the problem, but it does provide the exception object, which allows you to log the error and perform last-minute cleanup.
The following Console application uses this technique. Before it exits, it displays information about the offending error.
Public Module ErrorHandlerTest Public Sub Main() ' Connect a default ...