Click here to Skip to main content
15,884,177 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Exception Handling Library

Rate me:
Please Sign up or sign in to vote.
4.80/5 (4 votes)
9 Sep 2013CPOL2 min read 18.7K   356   11   3
A simple library for dealing with unhandled exceptions, with email support.

Image 1

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:

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

VB.NET
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:

VB.NET
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)


Written By
Founder SEOStart
United Kingdom United Kingdom
I'm a software and web developer based in London, United Kingdom. I am the founder of SEOStart, an SEO agency based in London, UK.

Comments and Discussions

 
QuestionNice and simple Pin
karenpayne23-Sep-13 10:27
karenpayne23-Sep-13 10:27 
GeneralMy vote of 5 Pin
Knight121910-Sep-13 3:57
Knight121910-Sep-13 3:57 
GeneralRe: My vote of 5 Pin
Anthony Daly10-Sep-13 6:57
Anthony Daly10-Sep-13 6:57 

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

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