TechnoTone
Junior Contributor
I'm a little nervous of this class that I have just written as it's doing some overloading and inheriting and as I'm new to all this I am unsure as to whether I've done it correctly. It all seems to work OK but I'd feel happier having some experts give it the once-over.
Here is the class in question:
Here is the GeneralUtilities class:
The idea of this is that when we get errors in our classes we can pass them back up the call-stack until we get to a position where we want to display the error. It will then show a complete list of all the levels involved in the error.
For example, this is the exception handler of a property:
...and this is the exception handler of a button click event which will display a message box showing the errors that occurred:
Here is the class in question:
Visual Basic:
Public Class bofaException
Inherits Exception
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal ex As Exception)
MyBase.New(GeneralUtilities.ExceptionString(ex))
End Sub
Public Sub New(ByVal ex As Exception, ByVal Source As String)
MyBase.New(GeneralUtilities.ExceptionString(ex, Source))
End Sub
Public Sub New(ByVal ex As Exception, ByVal Source As String, ByVal Message As String)
MyBase.New(GeneralUtilities.ExceptionString(ex, Source, Message))
End Sub
Public Sub New(ByVal ex As Exception, ByVal Source As String, ByVal Message As String, ByVal ShowStack As Boolean)
MyBase.New(GeneralUtilities.ExceptionString(ex, Source, Message, ShowStack))
End Sub
End Class
Here is the GeneralUtilities class:
Visual Basic:
Public Class GeneralUtilities
Shared Function ExceptionString(ByVal ex As Exception, Optional ByVal Source As String = "", Optional ByVal Message As String = "", Optional ByVal ShowStack As Boolean = False) As String
If Message <> "" Then Message &= Environment.NewLine
Message &= "Error source: " & Microsoft.VisualBasic.vbTab & ex.Source
If Source <> "" Then Message &= ": " & Source.Trim
If ShowStack And (ex.StackTrace.Length > 0) Then
Dim sPrefix As String
Dim sStackString As String
sPrefix = Environment.NewLine & "Stack trace: " & Microsoft.VisualBasic.vbTab
sStackString = ex.StackTrace.Trim
While sStackString.Length > 0
If sStackString.Substring(0, 2) = "at" Then
If sStackString.IndexOf(" in ") > 0 Then
Message &= sPrefix & sStackString.Substring(0, sStackString.IndexOf(" in ", 4)).Trim
sStackString = sStackString.Remove(0, sStackString.IndexOf(" in ", 4)).Trim
sPrefix = Environment.NewLine & " " & Microsoft.VisualBasic.vbTab
Else
Message &= sPrefix & sStackString
sStackString = ""
End If
End If
If sStackString.Substring(0, 2) = "in" Then
If sStackString.IndexOf(" at ") > 0 Then
Message &= sPrefix & sStackString.Substring(0, sStackString.IndexOf(" at ", 4)).Trim.Insert(3, " ")
sStackString = sStackString.Remove(0, sStackString.IndexOf(" at ", 4)).Trim
sPrefix = Environment.NewLine & " " & Microsoft.VisualBasic.vbTab
Else
Message &= sPrefix & sStackString.Insert(3, " ")
sStackString = ""
End If
End If
End While
End If
If ShowStack Then
Message &= Environment.NewLine & Environment.NewLine & "Error message: " & Microsoft.VisualBasic.vbTab & ex.Message
Else
Message &= Environment.NewLine & "Error message: " & Microsoft.VisualBasic.vbTab & ex.Message
End If
ExceptionString = Message
End Function
End Class
The idea of this is that when we get errors in our classes we can pass them back up the call-stack until we get to a position where we want to display the error. It will then show a complete list of all the levels involved in the error.
For example, this is the exception handler of a property:
Visual Basic:
Throw New bofaException(ex, "SecurityUtilities: PassApplicationID property set", "An error occurred while setting the property.", True)
...and this is the exception handler of a button click event which will display a message box showing the errors that occurred:
Visual Basic:
MessageBox.Show(GeneralUtilities.ExceptionString(ex, "ColumnHeaderButton1_Click", "OOPS!!!"))