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 4

De-Serialization Constructor
This is the constructor enforced by the Serializable token in front of the Class declaration.  It rebuilds all the class private members after a serialization and you may need to modify this constructors if you add or remove properties from this class.

    Public Sub New(ByVal info As System.Runtime.Serialization.SerializationInfo, _
                   ByVal context As System.Runtime.Serialization.StreamingContext)
        '
        MyBase.New(info, context)

        _ObjectCulture = info.GetString("ApplicationCulture")
        _ObjectVersion = info.GetString("ApplicationVersion")
        bCulture = info.GetBoolean("ApplicationCultureFlag")
        bVersion = info.GetBoolean("ApplicationVersionFlag")
        _MethodAction = info.GetString("MethodAction")
        _MachineName = info.GetString("MachineName")
        _UserName = info.GetString("UserName")
        _OSVersion = info.GetString("OSVersion")
        _ErrorDate = info.GetDateTime("ErrorDate")
        _ErrorTime = DirectCast(info.GetString("ErrorTime"), TimeSpan)

    
End Sub

Custom Constructors Region:
Custom constructors are an important factor when inheriting from the ApplicationException class.   Here is where you implement additional properties to your particular exception handling class and you may add as many as your solution requires.

We are introducing the MethodAction property as one of those additional properties this exception class supports.  When you throw an exception you can pass a message that explains what your code was trying to do.  If your method has to trigger more than one exception, your message should include some kind of description of the action takeing place when the error condition was found.   That is precisely why the MethodAction property came to life.   Developers working with this exception class can pass this additional information without messing too much with the exception's message.

The next two constructors provide support for the MethodAction property.

    Public Sub New(ByVal MethodAction As String, _
                   ByVal message As String)

        MyBase.New(message)
        _MethodAction = MethodAction

    End Sub

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

        MyBase.New(message, innerException)
        _MethodAction = MethodAction

    End Sub

As you can see, each one of these constructors takes a MethodAction parameter, which is assigned to the private member _MethodAction.   Later on the class exposes its value with the MethodAction property. I hope that the chosen names do not confuse you, as a parameter was named MethodAction, and a property goes with the same name.sad

The first constructor takes just the message parameter for the exception, while the second handles two, the message and the innerException.  This is a standard practice when implementing additional parameters into exception classes.

Properties Region:
Now is time to show the region where the properties' declaration takes place in this class.  The first thing to notice is that all these properties are Readonly. You don't want any developer working with your exception class to tamper with the exception attributes.

We already explained that the machine name, operating system version and user name properties come from the System.Environment name space.   What about the remaining properties, how are they coming to life? Well, the private method GetApplicationAttributes() takes care of them.

    Public ReadOnly Property ApplicationVersion() As String
        Get
            If bVersion = False Then
                GetApplicationAttributes()
            End If
            Return
_ObjectVersion
        End Get
    End Property

    Public
ReadOnly Property ApplicationCulture() As String
        Get
            If
bCulture = False Then
                GetApplicationAttributes()
            End If
            Return
_ObjectCulture
        End Get
    End Property

    Public
ReadOnly Property ErrorDate() As String
        Get
            Return
_ErrorDate.ToString("yyyy/MM/dd")
        End Get
    End Property

    Public
ReadOnly Property ErrorTime() As String
        Get
            Return
_ErrorTime.Hours.ToString & ":" & _ErrorTime.Minutes.ToString
        End Get
    End Property

    Public
ReadOnly Property MachineName() As String
        Get
            Return
_MachineName
        End Get
    End Property

    Public
ReadOnly Property Method() As String
        Get
            Return
MyBase.TargetSite.Name
        End Get
    End Property

    Public
ReadOnly Property MethodAction() As String
        Get
            Return
_MethodAction
        End Get
    End Property

    Public
ReadOnly Property OSVersion() As String
        Get
            Return
_OSVersion
        End Get
    End Property

    Public
Overrides Function ToString() As String

        Return
MyBase.ToString & vbCrLf & _
               "Source Object : " & Me.Source & vbCrLf & _
               "Source Method : " & Me.TargetSite.Name & vbCrLf & _
               "Method Action : " & _MethodAction

    End Function

    Public
ReadOnly Property UserName() As String
        Get
            Return
_UserName
        
End Get
    End Property

Take a look at the ToString function.   Yes, it is listed as a property, but readonly properties and functions are closely related.   A function behaves like a Readonly property and vice versa. The first thing to notice is: It is incomplete, there are properties missing, you can add them later, We didn't because we were implementing additional functionalities to the class, but the idea with the ToString function is that it should return all the class private members.

 

Sponsored Links