How to walk through InnerExceptions?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Terry Olsen

    How to walk through InnerExceptions?

    How can I walk through the InnerExceptions ? Would the following code be
    correct?

    Private Sub ShowException(B yVal ex As Exception)
    MsgBox(ex.Messa ge)
    If ex.InnerExcepti on Is Nothing = False Then _
    ShowException(e x.InnerExceptio n)
    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 ***
  • GhostInAK

    #2
    Re: How to walk through InnerExceptions ?

    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 GetInnerExcepti on(byref tException As Exception) As String

    Dim tReturn As String = String.Empty

    If Not Nothing Is tException Then

    tReturn = tException.ToSt ring()

    If Not Nothing Is tException.Inne rException Then
    tReturn = tReturn & GetInnerExcepti on(tException.I nnerException)
    End If

    End If

    Return tReturn

    End Function
    [color=blue]
    > How can I walk through the InnerExceptions ? Would the following code
    > be correct?
    >
    > Private Sub ShowException(B yVal ex As Exception)
    > MsgBox(ex.Messa ge)
    > If ex.InnerExcepti on Is Nothing = False Then _
    > ShowException(e x.InnerExceptio n)
    > 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 ***
    >[/color]


    Comment

    • Göran Andersson

      #3
      Re: How to walk through InnerExceptions ?

      No, there is no need for a recursive function. It can be done just as
      easily using a simple loop:

      Private Function GetInnerExcepti on(byref tException As Exception) As String

      Dim tReturn As String = String.Empty

      Do While Not tException Is Nothing
      tReturn &= tException.ToSt ring()
      tException = tException.Inne rException
      Loop

      Return tReturn

      End Function


      Save the recursion for when it's useful. :)


      GhostInAK wrote:[color=blue]
      > 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 GetInnerExcepti on(byref tException As Exception) As String
      >
      > Dim tReturn As String = String.Empty
      >
      > If Not Nothing Is tException Then
      >
      > tReturn = tException.ToSt ring()
      >
      > If Not Nothing Is tException.Inne rException Then
      > tReturn = tReturn & GetInnerExcepti on(tException.I nnerException)
      > End If
      >
      > End If
      >
      > Return tReturn
      >
      > End Function
      >[color=green]
      >> How can I walk through the InnerExceptions ? Would the following code
      >> be correct?
      >>
      >> Private Sub ShowException(B yVal ex As Exception)
      >> MsgBox(ex.Messa ge)
      >> If ex.InnerExcepti on Is Nothing = False Then _
      >> ShowException(e x.InnerExceptio n)
      >> 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 ***
      >>[/color]
      >
      >[/color]

      Comment

      • Terry Olsen

        #4
        Re: How to walk through InnerExceptions ?

        Thanks. I knew recursion wasn't necessary because I'd seen it before
        done this way.

        *** Sent via Developersdex http://www.developersdex.com ***

        Comment

        • GhostInAK

          #5
          Re: How to walk through InnerExceptions ?

          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
          [color=blue]
          > No, there is no need for a recursive function. It can be done just as
          > easily using a simple loop:
          >
          > Private Function GetInnerExcepti on(byref tException As Exception) As
          > String
          >
          > Dim tReturn As String = String.Empty
          >
          > Do While Not tException Is Nothing
          > tReturn &= tException.ToSt ring()
          > tException = tException.Inne rException
          > Loop
          > Return tReturn
          >
          > End Function
          >
          > Save the recursion for when it's useful. :)
          >
          > GhostInAK wrote:
          >[color=green]
          >> 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 GetInnerExcepti on(byref tException As Exception) As
          >> String
          >>
          >> Dim tReturn As String = String.Empty
          >>
          >> If Not Nothing Is tException Then
          >>
          >> tReturn = tException.ToSt ring()
          >>
          >> If Not Nothing Is tException.Inne rException Then
          >> tReturn = tReturn & GetInnerExcepti on(tException.I nnerException)
          >> End If
          >> End If
          >>
          >> Return tReturn
          >>
          >> End Function
          >>[color=darkred]
          >>> How can I walk through the InnerExceptions ? Would the following code
          >>> be correct?
          >>>
          >>> Private Sub ShowException(B yVal ex As Exception)
          >>> MsgBox(ex.Messa ge)
          >>> If ex.InnerExcepti on Is Nothing = False Then _
          >>> ShowException(e x.InnerExceptio n)
          >>> 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 ***
          >>>[/color][/color][/color]


          Comment

          • Göran Andersson

            #6
            Re: How to walk through InnerExceptions ?

            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 GetInnerExcepti on(tException As Exception) As String

            Dim tReturn As String = String.Empty

            Do While Not tException Is Nothing
            tReturn &= tException.ToSt ring()
            tException = tException.Inne rException
            Loop

            Return tReturn

            End Function


            GhostInAK wrote:[color=blue]
            > 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
            >[color=green]
            >> No, there is no need for a recursive function. It can be done just as
            >> easily using a simple loop:
            >>
            >> Private Function GetInnerExcepti on(byref tException As Exception) As
            >> String
            >>
            >> Dim tReturn As String = String.Empty
            >>
            >> Do While Not tException Is Nothing
            >> tReturn &= tException.ToSt ring()
            >> tException = tException.Inne rException
            >> Loop
            >> Return tReturn
            >>
            >> End Function
            >>
            >> Save the recursion for when it's useful. :)
            >>
            >> GhostInAK wrote:
            >>[color=darkred]
            >>> 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 GetInnerExcepti on(byref tException As Exception) As
            >>> String
            >>>
            >>> Dim tReturn As String = String.Empty
            >>>
            >>> If Not Nothing Is tException Then
            >>>
            >>> tReturn = tException.ToSt ring()
            >>>
            >>> If Not Nothing Is tException.Inne rException Then
            >>> tReturn = tReturn & GetInnerExcepti on(tException.I nnerException)
            >>> End If
            >>> End If
            >>>
            >>> Return tReturn
            >>>
            >>> End Function
            >>>
            >>>> How can I walk through the InnerExceptions ? Would the following code
            >>>> be correct?
            >>>>
            >>>> Private Sub ShowException(B yVal ex As Exception)
            >>>> MsgBox(ex.Messa ge)
            >>>> If ex.InnerExcepti on Is Nothing = False Then _
            >>>> ShowException(e x.InnerExceptio n)
            >>>> 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 ***
            >>>>[/color][/color]
            >
            >[/color]

            Comment

            Working...