|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to walk through InnerExceptions?How can I walk through the InnerExceptions? Would the following code be
correct? Private Sub ShowException(ByVal ex As Exception) MsgBox(ex.Message) If ex.InnerException Is Nothing = False Then _ ShowException(ex.InnerException) End Sub I thought I saw a snippet a while ago using a For...Each loop, but I'm unable to find it again. *** Sent via Developersdex http://www.developersdex.com *** Hello Terry,
I've not had call to walk over inner exceptions, however, if you really want to, then you'll need to use a recursive function. Private Function GetInnerException(byref tException As Exception) As String Dim tReturn As String = String.Empty If Not Nothing Is tException Then tReturn = tException.ToString() If Not Nothing Is tException.InnerException Then tReturn = tReturn & GetInnerException(tException.InnerException) End If End If Return tReturn End Function Show quoteHide quote > How can I walk through the InnerExceptions? Would the following code > be correct? > > Private Sub ShowException(ByVal ex As Exception) > MsgBox(ex.Message) > If ex.InnerException Is Nothing = False Then _ > ShowException(ex.InnerException) > End Sub > I thought I saw a snippet a while ago using a For...Each loop, but I'm > unable to find it again. > > *** Sent via Developersdex http://www.developersdex.com *** > No, there is no need for a recursive function. It can be done just as
easily using a simple loop: Private Function GetInnerException(byref tException As Exception) As String Dim tReturn As String = String.Empty Do While Not tException Is Nothing tReturn &= tException.ToString() tException = tException.InnerException Loop Return tReturn End Function Save the recursion for when it's useful. :) GhostInAK wrote: Show quoteHide quote > Hello Terry, > > I've not had call to walk over inner exceptions, however, if you really > want to, then you'll need to use a recursive function. > > Private Function GetInnerException(byref tException As Exception) As String > > Dim tReturn As String = String.Empty > > If Not Nothing Is tException Then > > tReturn = tException.ToString() > > If Not Nothing Is tException.InnerException Then > tReturn = tReturn & GetInnerException(tException.InnerException) > End If > > End If > > Return tReturn > > End Function > >> How can I walk through the InnerExceptions? Would the following code >> be correct? >> >> Private Sub ShowException(ByVal ex As Exception) >> MsgBox(ex.Message) >> If ex.InnerException Is Nothing = False Then _ >> ShowException(ex.InnerException) >> End Sub >> I thought I saw a snippet a while ago using a For...Each loop, but I'm >> unable to find it again. >> >> *** Sent via Developersdex http://www.developersdex.com *** >> > > Thanks. I knew recursion wasn't necessary because I'd seen it before
done this way. *** Sent via Developersdex http://www.developersdex.com *** Hello Göran,
No no no. tException is declared ByRef. You dont wanna be changing it's value willy-nilly like that. If you want to use a loop like that then pass the exception ByVal. -Boo Show quoteHide quote > No, there is no need for a recursive function. It can be done just as > easily using a simple loop: > > Private Function GetInnerException(byref tException As Exception) As > String > > Dim tReturn As String = String.Empty > > Do While Not tException Is Nothing > tReturn &= tException.ToString() > tException = tException.InnerException > Loop > Return tReturn > > End Function > > Save the recursion for when it's useful. :) > > GhostInAK wrote: > >> Hello Terry, >> >> I've not had call to walk over inner exceptions, however, if you >> really want to, then you'll need to use a recursive function. >> >> Private Function GetInnerException(byref tException As Exception) As >> String >> >> Dim tReturn As String = String.Empty >> >> If Not Nothing Is tException Then >> >> tReturn = tException.ToString() >> >> If Not Nothing Is tException.InnerException Then >> tReturn = tReturn & GetInnerException(tException.InnerException) >> End If >> End If >> >> Return tReturn >> >> End Function >> >>> How can I walk through the InnerExceptions? Would the following code >>> be correct? >>> >>> Private Sub ShowException(ByVal ex As Exception) >>> MsgBox(ex.Message) >>> If ex.InnerException Is Nothing = False Then _ >>> ShowException(ex.InnerException) >>> End Sub >>> I thought I saw a snippet a while ago using a For...Each loop, but >>> I'm >>> unable to find it again. >>> *** Sent via Developersdex http://www.developersdex.com *** >>> You are right. I didn't notice the byref when I copied the code. There
is no reason to send the reference to the exception by reference. Simply remove the byref from the method: Private Function GetInnerException(tException As Exception) As String Dim tReturn As String = String.Empty Do While Not tException Is Nothing tReturn &= tException.ToString() tException = tException.InnerException Loop Return tReturn End Function GhostInAK wrote: Show quoteHide quote > Hello Göran, > > No no no. tException is declared ByRef. You dont wanna be changing > it's value willy-nilly like that. If you want to use a loop like that > then pass the exception ByVal. > > -Boo > >> No, there is no need for a recursive function. It can be done just as >> easily using a simple loop: >> >> Private Function GetInnerException(byref tException As Exception) As >> String >> >> Dim tReturn As String = String.Empty >> >> Do While Not tException Is Nothing >> tReturn &= tException.ToString() >> tException = tException.InnerException >> Loop >> Return tReturn >> >> End Function >> >> Save the recursion for when it's useful. :) >> >> GhostInAK wrote: >> >>> Hello Terry, >>> >>> I've not had call to walk over inner exceptions, however, if you >>> really want to, then you'll need to use a recursive function. >>> >>> Private Function GetInnerException(byref tException As Exception) As >>> String >>> >>> Dim tReturn As String = String.Empty >>> >>> If Not Nothing Is tException Then >>> >>> tReturn = tException.ToString() >>> >>> If Not Nothing Is tException.InnerException Then >>> tReturn = tReturn & GetInnerException(tException.InnerException) >>> End If >>> End If >>> >>> Return tReturn >>> >>> End Function >>> >>>> How can I walk through the InnerExceptions? Would the following code >>>> be correct? >>>> >>>> Private Sub ShowException(ByVal ex As Exception) >>>> MsgBox(ex.Message) >>>> If ex.InnerException Is Nothing = False Then _ >>>> ShowException(ex.InnerException) >>>> End Sub >>>> I thought I saw a snippet a while ago using a For...Each loop, but >>>> I'm >>>> unable to find it again. >>>> *** Sent via Developersdex http://www.developersdex.com *** >>>> > >
Why Me? (Instead of Form1)
How do I increment a byte with out casting? Capturing mouse events (Mouse up and down on the desktop) Loading CrystalReports.rpt, I Am Asked To Enter Login ID And Password DAAB problem most natural behavior on mouse wheel Cancel Constructor (Me = Nothing) Threading in .Net... Parent child binding question RichTextBox in Vs2005 Vb.Net shows unformatted RTF |
|||||||||||||||||||||||