Article Options
Premium Sponsor
Premium Sponsor

Structured Exception Handling in .NET (ApplicationException)
by Arnaldo Sandoval | Published  11/04/2006 | .NET Framework .NET Intermediate Windows Development | Rating:
Page 2

     a = b / c
Catch e1 As SEHComponent.ErrorHandlingException
Catch se As SystemException
   MessageBox.Show(se.Message, "System Exception", MessageBoxButtons.OK, _
End Try

This time the OverflowException is trapped by the second catch, the SystemException one! This is happening because ALL the exception classes are children of the SystemException class, including the ApplicationException class.  For this simple reason, the code shown above will display the following error message to the user.


It looks nice and clean, no end user will be intimidated with a message like this.


A popup error message is a nice way to tell our application's user about any unexpected error in the application.  They are less frightening than those returned by the runtime.   Nevertheless, it has its shortcoming if your application is not recording key information about the exception at the time it happens and you are expecting the user to write the details down on a piece of paper.  In this case you should train him/her on what information is meaningful to you, like the message (Arithmetic operation resulted in an overflow), the popup form title (sometimes: System Exception) and the form he/she was working on when the exception happens (Form1). Sometimes they do a great job collecting this info for you, but there are risks you don't get the whole picture.

Your application is aware of a lot of things at the time an exception happens.  Wouldn't it be nice if you could write that information to a file or database, or even email it to you, instead of relying on the end user help. The message shown below is not very user friendly but it illustrates what exception classes could deliver as a whole into your applications.

Well, the message above tells us a lot of technical things when the error happened, but as previously said, it is not user friendly.   However you can write all this information to a file or database, including the date and time it happened.  You could even email it to yourself, or perhaps send an SMS message with all this information.

THE ErrorHandlingException CLASS

In the next section of this article we meet the ErrorHandlingException class, a class based on the ApplicationException class.   It has the following features.

  • Collect the date and time of the exception.
  • Identifies the Machine, Operating System, and User.
  • Identifies the application name, application version, culture and method.
  • Accept an Action parameter, where you can pass a more specific explanation of what your application was doing at the time of the exception.
  • It does not hide any existing property or method provided by its parent class: ApplicationException.
  • Write these exception's details to a log file or database (Access).

Beside the features listed above, you can expand it, implementing new functionalities, like:

  • Send emails or SMS messages containing the exception details.
  • You can easily write the exception's details to other database (MS SQL, Oracle)
  • Its project and source code is included, so you can enhance it to suit your requirements.

Now, we will walk thru the code for the ErrorHandlingException class, explaining its most relevant properties and methods. The project ErrorHandlingException (same name) contains the mentioned class, and it is assigned to the namespace SEHComponent (Structured Exception Handling Component).

Imports System.Data.OleDb
Imports System.Configuration.ConfigurationSettings

Sponsored Links