6.2. Unhandled Exception Handler

The stack trace that results from an unhandled exception is a default handler. It's a nice feature, but it is not very robust. It might work well for grandma's little recipe program, but in a bulletproof, industrial-strength application, it just won't do. Errors need to be logged to the event log or a database, emails need to be sent to technical support, and programmers need to be paged in the wee hours o' the morning. More than likely, you will probably need to use the stack trace information in conjunction with other forms of error reporting.

Fortunately, the .NET Framework is very giving in this area. You are free to substitute your own handler for unhandled exceptions, as demonstrated in Example 6-4.

Note the call to the AppDomain.CurrentDomain method in the Main procedure in Example 6-4. Calling AppDomain.CurrentDomain returns an object that represents the current application domain. Recall from Section 2.4 of Chapter 2 that an application domain is the execution environment for a .NET assembly. The instance of AppDomain that is returned as a result of this call provides a number of interesting properties and methods. Here are a few:

BaseDirectory property

Returns the path of the application. It is similar to App.Path in earlier versions of VB.

FriendlyName property

Returns the name of the executable. It is similar to App.EXEName in earlier versions of VB.

ExecuteAssembly method

Executes the specified assembly. It is similar to ...

Get Object-Oriented Programming with Visual Basic .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.