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

Try
     a = b / c
Catch e1 As SEHComponent.ErrorHandlingException
   ShowError(e1)
Catch se As SystemException
   MessageBox.Show(se.Message, "System Exception", MessageBoxButtons.OK, _
   MessageBoxIcon.Error)
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.

POP-UP ERROR MESSAGES SIDE EFFECTS

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

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 11/16/2006)
Rating
The article was in-depth and provided an excellent amount of information.

Chas
 
Comment #2  (Posted by an unknown user on 11/21/2006)
Rating
It was very well designed and useful to understand how I can add this to my systems. However, after moving the TargetErrorLog.mdb to c:\temp and fixing the reference to ErrorHandlingException.dll, the program will only run correctly within VB.net and not as a stand alone exe. I did build the solution and the program. The error I get when running it outside of VB.Net is: File or assembly name ErrorHandlingException, or one of its dependencies, was not found. Maybe this shows my inexperience with VB.Net?
 
Comment #3  (Posted by an unknown user on 11/21/2006)
Rating
It was very well designed and useful to understand how I can add this to my systems. However, after moving the TargetErrorLog.mdb to c:\temp and fixing the reference to ErrorHandlingException.dll, the program will only run correctly within VB.net and not as a stand alone exe. I did build the solution and the program. The error I get when running it outside of VB.Net is: File or assembly name ErrorHandlingException, or one of its dependencies, was not found. Maybe this shows my inexperience with VB.Net?
 
Comment #4  (Posted by Morten Grøtan on 10/29/2007)
Rating
The GetApplicationAttributes method can be greatly simplified by using the AssemblyName object instead of the AssemblyQualifiedName, using this (C#) code:

// C# "short-circuit" mechanism makes sure that the if-check exits at the first false expression
if ((targetSite != null) && (targetSite.ReflectedType != null) && (targetSite.ReflectedType.Assembly != null))
{
// This is the same info as in AssemblyQualifiedName, only represented as an object
AssemblyName assemblyName = targetSite.ReflectedType.Assembly.GetName();

Version version = assemblyName.Version;
if (version != null)
{
_objectVersion = version.ToString();
foundVersion = true;
}

CultureInfo culture = assemblyName.CultureInfo;
if ((culture != null) && (culture.ToString().Length > 0))
{
_objectCulture = culture.ToString();
foundCulture = true;
}
}

 
Sponsored Links