Click here to Skip to main content
Click here to Skip to main content

Exception Handling Library

, 9 Sep 2013
Rate this:
Please Sign up or sign in to vote.
A simple library for dealing with unhandled exceptions, with email support.

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
	'Make a new emailer object
	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}
 
  	'Send the exception	
	e.SendException(ex)
 
	'Show an alert
	MessageBox.Show("An unhandled exception has occurred and the application" & _ 
	  " will now close.", "Application error", _
	  MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
 
	'Close application
	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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Anthony Daly
Founder SoftwareStats
United Kingdom United Kingdom
I'm a software and web developer based in London, United Kingdom. I have experience in a variety of technologies, with a particular interest in Windows Forms applications, and PHP web applications. I am the founder of SoftwareStats, a Runtime Intelligence and Software Analytics service allowing .NET developers to track their applications.
Follow on   Twitter

Comments and Discussions

 
QuestionNice and simple Pinmemberkevininstructor23-Sep-13 10:27 
GeneralMy vote of 5 PinmemberKnight121910-Sep-13 3:57 
GeneralRe: My vote of 5 PinmemberAnthony Daly10-Sep-13 6:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 9 Sep 2013
Article Copyright 2013 by Anthony Daly
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid