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 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:




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)


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


  • 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
    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()


    End Sub

    Public Sub New(ByVal message As String)


    End Sub

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

        MyBase.New(message, innerException)

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.


Sponsored Links