Article Options
Recently Viewed
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 3

The two imported namespaces are required by the methods handling data access and the application configuration file. You should import the namespaces for MS SQL and/or Oracle if you wish to extend this class by implementing either database to store the ExceptionLog data base.

<SERIALIZABLE()>Public Class ErrorHandlingException

   Inherits System.ApplicationException

The class declaration is preceded by the Serializable token and a couple of methods are required to implement its serialization.

Following Microsoft recommendation, it inherits the System.ApplicationException class, which enables the class to be used in exception trapping (ApplicationException trapping). The ApplicationException class exposes the following constructors, methods and properties:

Constructors:

Methods:

Properties:

Those with an asterisk (*) are the ones this implementation cater for by providing additional code. Beside the class members previously listed, we are also implementing these ones:

Custom Constructors:

  • ApplicationException Constructor (Method Action As String, message As String)
  • ApplicationException Constructor (Method Action As String, message As String, innerException)

Methods:

  • Private Clear()
  • Private GetApplicationAttributes()
  • Public GetExceptions()
  • Public Save(TargetErrorLog, TargetOutput)
  • Private SaveToFile(TargetOutput)
  • Private SaveToAccess(TargetOutput)

Properties:

  • ApplicationVersion
  • ApplicationCulture
  • ErrorDate
  • ErrorTime
  • MachineName
  • Method
  • MethodAction
  • OSVersion

Variables Declaration Region:
This is the first region in our class code.   Here all the private and public members required by the class' properties and methods are declared, as shown:

        Private _ObjectVersion As String = "0.0"
    Private bVersion As Boolean = False
    Private _ObjectCulture As String = "Unknown"
    Private bCulture As Boolean = False
    Private _MethodAction As String = "Unknown"
    Private _UserName As String = System.Environment.UserName
    Private _MachineName As String = System.Environment.MachineName
    Private _OSVersion As String = System.Environment.OSVersion.ToString
    Private _ErrorDate As Date
    Private _ErrorTime As TimeSpan

    Public Enum TargetErrorLogs
        FlatFile
        Access
    End Enum

    Const DEFAULT_CONNECTION_STRING As String = "DBConnStringErrorHandlingException"

You may notice that we are using the System.Environment namespace to obtain the user name, machine name and operating system version>   Your exception handling requirements could be different and so you may need to use other namespaces to implement additional properties.

Standard Constructors Region:
These are the constructors you may never have to change.

    Public Sub New()

        MyBase.New()
        Me.Clear()

    End Sub

    Public Sub New(ByVal message As String)

        MyBase.New(message)
        Me.Clear()

    End Sub

    Public Sub New(ByVal message As String, _
                   ByVal innerException As SystemException)

        MyBase.New(message, innerException)
        Me.Clear()

    
End Sub

There is nothing fancy in these constructors, perhaps the only one needed more explanation being the reference to the private method Clear() that is explained further down this post.

 

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;
}
}

 
Comment #5  (Posted by Sumit on 10/17/2008)
Rating
Very useful info.
Thanx
 
Comment #6  (Posted by Buy oem software on 09/29/2011)
Rating
EqdRl5 Hello! How do you feel about young composers?!...
 
Sponsored Links