Get an exceptions full stack trace with inner ex





3
Date Submitted Sat. Sep. 9th, 2006 11:10 AM
Revision 1 of 1
Helper jbplou
Tags Exception | VB.NET
Comments 1 comments
This function returns a stack trace for an exception with all inner exceptions include unless the count of inner exceptions goes over 50. A stack overflow can occur if there is no upper limit for inner exceptions to read from.

Makes use of stringbuilder to build return string in order to yield higher performance.

    'Returns a string representing a full stack for a passed in exception and all inner exceptions
    'will only get the first 50 inner exceptions to prevent stack overflow
    Private Function fullStackTrace(ByVal ex As Exception) As String
        If ex Is Nothing Then
            Throw New ArgumentException("ex can not be null", "ex")
        End If
        Dim outputStack As New System.Text.StringBuilder
        outputStack.Append(ex.StackTrace)
        Dim innerReferences As Byte = 0 'used to ensure memory does not run out
        'during stack looping
        Dim innerException As Exception = ex.InnerException
        While Not innerException Is Nothing _
            AndAlso innerReferences < 50
            outputStack.Insert(0, innerException.StackTrace)
            innerException = innerException.InnerException
            innerReferences += CByte(1)
        End While
        Return outputStack.ToString
    End Function
 

Jeff B

Comments

Comments Not bad....
Fri. Jun. 1st, 2007 2:21 PM    Beginner Jax

Voting