473,385 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
5 7536
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Marcello Pietrobon | last post by:
Hello, I am using Pyton 2.3 I desire to walk a directory without recursion this only partly works: def walk_files() : for root, dirs, files in os.walk(top, topdown=True): for filename in...
6
by: rbt | last post by:
More of an OS question than a Python question, but it is Python related so here goes: When I do os.walk('/') on a Linux computer, the entire file system is walked. On windows, however, I can...
7
by: KraftDiner | last post by:
The os.walk function walks the operating systems directory tree. This seems to work, but I don't quite understand the tupple that is returned... Can someone explain please? for root, dirs,...
6
by: Bruce | last post by:
Hi all, I have a question about traversing file systems, and could use some help. Because of directories with many files in them, os.walk appears to be rather slow. I`m thinking there is a...
9
by: silverburgh.meryl | last post by:
i am trying to use python to walk thru each subdirectory from a top directory. Here is my script: savedPagesDirectory = "/home/meryl/saved_pages/data" dir=open(savedPagesDirectory, 'r') ...
2
by: gregpinero | last post by:
In the example from help(os.walk) it lists this: from os.path import join, getsize for root, dirs, files in walk('python/Lib/email'): print root, "consumes", print sum(), print "bytes in",...
2
by: Martin Marcher | last post by:
Hello, I'm playing around with os.walk and I made up del_tree(path) which I think is correct (in terms of the algorithm, but not as python wants it :)). As soon as some directory is deleted...
0
by: Jeff McNeil | last post by:
Your args are fine, that's just the way os.path.walk works. If you just need the absolute pathname of a directory when given a relative path, you can always use os.path.abspath, too. A couple...
4
by: Jeff Nyman | last post by:
Greetings all. I did some searching on this but I can't seem to find a specific solution. I have code like this: ========================================= def walker1(arg, dirname, names):...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.