By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,064 Members | 1,542 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,064 IT Pros & Developers. It's quick & easy.

On error resume next bug

P: n/a
Neo
I found on error resume next doesn't work in for each... e.g.

on error resume next
for each x in y
'do stuff
next

if you have an error in for each loop, it falls in infinite loop... it
doesn't move to next element... this sad thing but, it's indication
that we must move to try catch instead of on error.
-Neo

Jul 17 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
It works for me.

'VB 2005
Sub Main()
On Error Resume Next
Dim c As New Collection
c.Add(1)
c.Add(2)
c.Add("A")
c.Add(3)
c.Add("B")
Dim i As Integer
Dim v As Object
i = 0
For Each v In c
i = i + v
Debug.Print(i)
Next
Debug.Print(i)
End Sub

I wrote and tested this code in both VB 6 and 2005. The real issue here is
the use of the On Error Resume Next statement. It should be replaced
whereever feasible with try catch statements.

Mike.

"Neo" <pr*********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
I found on error resume next doesn't work in for each... e.g.

on error resume next
for each x in y
'do stuff
next

if you have an error in for each loop, it falls in infinite loop... it
doesn't move to next element... this sad thing but, it's indication
that we must move to try catch instead of on error.
-Neo

Jul 17 '06 #2

P: n/a
Neo
But does it really raise error? If yes, after error does it move to
next element?

My error was during traversing graphs collection in excel worksheet.The
problem I had when there was an error which made the infinte loop. I am
not sure if that was only to do with that particualar access, bottom
line is say bye, bye to "on error resume next"

Michael D. Ober wrote:
It works for me.

'VB 2005
Sub Main()
On Error Resume Next
Dim c As New Collection
c.Add(1)
c.Add(2)
c.Add("A")
c.Add(3)
c.Add("B")
Dim i As Integer
Dim v As Object
i = 0
For Each v In c
i = i + v
Debug.Print(i)
Next
Debug.Print(i)
End Sub

I wrote and tested this code in both VB 6 and 2005. The real issue here is
the use of the On Error Resume Next statement. It should be replaced
whereever feasible with try catch statements.

Mike.

"Neo" <pr*********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
I found on error resume next doesn't work in for each... e.g.

on error resume next
for each x in y
'do stuff
next

if you have an error in for each loop, it falls in infinite loop... it
doesn't move to next element... this sad thing but, it's indication
that we must move to try catch instead of on error.
-Neo
Jul 17 '06 #3

P: n/a
Yes, it raises a "First Chance Exception" and then loops to the next item.
I don't know if the Excel Graphs collection is a true COM collection. You
may be better off using

For i as Integer = 0 to Graphs.Count -1
dim gr as Excel.Graph = Graphs(i)
' Do something with gr
next i

Having worked with Excel, I have discovered that some of Excel's collections
are true COM collections and others require using the count method above.

You are correct in that you really need to dump On Error Resume Next. You
should also add "Option Explicit On" and "Option Strict On" at the top of
all your code modules. This will allow the VB compiler to find and flag all
sorts of variable typing errors. You'll be surprised at how many common VB
6 constructs are simply dangerous - Option Strict On will catch 99+% of
them.

Mike.
"Neo" <pr*********@gmail.comwrote in message
news:11**********************@35g2000cwc.googlegro ups.com...
But does it really raise error? If yes, after error does it move to
next element?

My error was during traversing graphs collection in excel worksheet.The
problem I had when there was an error which made the infinte loop. I am
not sure if that was only to do with that particualar access, bottom
line is say bye, bye to "on error resume next"

Michael D. Ober wrote:
It works for me.

'VB 2005
Sub Main()
On Error Resume Next
Dim c As New Collection
c.Add(1)
c.Add(2)
c.Add("A")
c.Add(3)
c.Add("B")
Dim i As Integer
Dim v As Object
i = 0
For Each v In c
i = i + v
Debug.Print(i)
Next
Debug.Print(i)
End Sub

I wrote and tested this code in both VB 6 and 2005. The real issue here
is
the use of the On Error Resume Next statement. It should be replaced
whereever feasible with try catch statements.

Mike.

"Neo" <pr*********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
I found on error resume next doesn't work in for each... e.g.
>
on error resume next
for each x in y
'do stuff
next
>
if you have an error in for each loop, it falls in infinite loop... it
doesn't move to next element... this sad thing but, it's indication
that we must move to try catch instead of on error.
-Neo
>


Jul 18 '06 #4

P: n/a
Neo
Yes, I fully agree with you. I was still usin "on error resume next"
since it was easier. but if it doesnt' work even in one case, shouldn't
be used. Also, once you "on error" you can't use try catch.. that sux.
better not to use "on error".

about option strict and option explicit. I have been always using them.
Thanks for suggesting that, it might be useful to other VB 6 developer
who don't use it.
-Neo
Michael D. Ober wrote:
Yes, it raises a "First Chance Exception" and then loops to the next item.
I don't know if the Excel Graphs collection is a true COM collection. You
may be better off using

For i as Integer = 0 to Graphs.Count -1
dim gr as Excel.Graph = Graphs(i)
' Do something with gr
next i

Having worked with Excel, I have discovered that some of Excel's collections
are true COM collections and others require using the count method above.

You are correct in that you really need to dump On Error Resume Next. You
should also add "Option Explicit On" and "Option Strict On" at the top of
all your code modules. This will allow the VB compiler to find and flag all
sorts of variable typing errors. You'll be surprised at how many common VB
6 constructs are simply dangerous - Option Strict On will catch 99+% of
them.

Mike.
"Neo" <pr*********@gmail.comwrote in message
news:11**********************@35g2000cwc.googlegro ups.com...
But does it really raise error? If yes, after error does it move to
next element?

My error was during traversing graphs collection in excel worksheet.The
problem I had when there was an error which made the infinte loop. I am
not sure if that was only to do with that particualar access, bottom
line is say bye, bye to "on error resume next"

Michael D. Ober wrote:
It works for me.
>
'VB 2005
Sub Main()
On Error Resume Next
Dim c As New Collection
c.Add(1)
c.Add(2)
c.Add("A")
c.Add(3)
c.Add("B")
Dim i As Integer
Dim v As Object
i = 0
For Each v In c
i = i + v
Debug.Print(i)
Next
Debug.Print(i)
End Sub
>
I wrote and tested this code in both VB 6 and 2005. The real issue here
is
the use of the On Error Resume Next statement. It should be replaced
whereever feasible with try catch statements.
>
Mike.
>
"Neo" <pr*********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
I found on error resume next doesn't work in for each... e.g.

on error resume next
for each x in y
'do stuff
next

if you have an error in for each loop, it falls in infinite loop... it
doesn't move to next element... this sad thing but, it's indication
that we must move to try catch instead of on error.
-Neo
Aug 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.