O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Catch Unhandled Errors

Problem

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.

Solution

Create an event handler for the AppDomain.UnhandledException event.

Discussion

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required