Introduction
The Exception Handling Library (EHL) is a wrapper over the Application.ThreadException
and AppDomain.CurrentDomain.UnhandledException
events, providing a single ApplicationException
event that developers can subscribe to. The EHL also provides an email system, which sends the exception to a specified email address when it occurs.
Background
I wrote this article when I realised that all of my projects dealt with unhandled exceptions in a similar way - I would log the error and/or send it through email or a web service, and show the user a dialog window informing them of the error and that the application must now close.
I wanted to create a simple library that could be built upon for a variety of scenarios, still giving the developer the freedom of dealing with the exception, but providing some common actions such as emailing errors.
Using the code
The EHL is centered around the ExceptionHandler
class. This class provides all of the functionality of catching unhandled exceptions.
Add a reference to the 'Exception Handling Library.dll' file to start using the library in your project.
To use the ExceptionHandler
, create a new global variable. Remember to Import
EHL:
Private WithEvents eh As New ExceptionHandler
The WithEvents
keyword is needed because the ExceptionHandler
provides an ApplicationException
event, which is called whenever an unhandled exception occurs.
This variable should be created as early on as possible in the application, ideally before the first form opens (for example, by overriding the OnStartup
function of My.Application
).
You now need to initialize the ExceptionHandler
to tell it to register its own event handlers.
eh.Initialize()
The ExceptionHandler
is now monitoring your application and will raise its own ApplicationException
event when an error occurs - you can handle this event to be notified of unhandled exceptions:
Private Sub HandleEx(ByVal ex As Exception) Handles eh.ApplicationException
Dim e As New Emailer With {.FromAddress = tbFromAddress.Text,
.FromName = tbFromName.Text,
.ToAddress = tbToAddress.Text,
.ToName = tbToName.Text,
.Host = tbSMTPHost.Text,
.Port = tbPort.Text,
.Password = tbPass.Text}
e.SendException(ex)
MessageBox.Show("An unhandled exception has occurred and the application" & _
" will now close.", "Application error", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Application.Exit()
End Sub
The example event handler above emails the exception to a developer using the SendException
method of the Emailer
object. The Emailer
supports most email providers - you can pass in values for from/to addresses and names, host, port and your email password.
After an informative message is displayed, the application is closed. All this behaviour can be modified to fit your application's needs, the EHL just provides the event for you to handle as you wish.
The Demo Application
The demo application is a form with textboxes for you to input your email details. Clicking 'Throw ex' simulates an InvalidCastException
, sending it to the email address specified.
Feature 'Todo' List
- Support for logging of errors
- Support for a WCF web service to send exceptions for storage/processing server-side
- Support for getting the user's details for more information
History
- 9 September 2013 - First version of EHL.