467,888 Members | 1,358 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,888 developers. It's quick & easy.

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 ***
Jun 6 '06 #1
  • viewed: 7242
Share:
5 Replies
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 ***

Jun 6 '06 #2
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 ***


Jun 6 '06 #3
Thanks. I knew recursion wasn't necessary because I'd seen it before
done this way.

*** Sent via Developersdex http://www.developersdex.com ***
Jun 6 '06 #4
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 ***

Jun 7 '06 #5
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:
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 ***


Jun 7 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Marcello Pietrobon | last post: by
6 posts views Thread by rbt | last post: by
7 posts views Thread by KraftDiner | last post: by
6 posts views Thread by Bruce | last post: by
9 posts views Thread by silverburgh.meryl | last post: by
2 posts views Thread by gregpinero | last post: by
2 posts views Thread by Martin Marcher | last post: by
reply views Thread by Jeff McNeil | last post: by
4 posts views Thread by Jeff Nyman | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.