error label and for next loop

Cableguy

Expert
Licensed User
Hi,

I have an Error Label inside a for next loop, and when an error is trapped the error label is called but the for next loop is not exited and becomes an endless loop...

B4X:
Sub MySub

errorlabel (error)

for x = 0 to 49
D Something
next x

error:
msgbox("file was not created!!",,cMsgBoxOK,cMsgBoxExclamation)

End Sub
How can I exit the loop when the error is trapped?
I've tryed Exit and Return with no success!!
 

specci48

Well-Known Member
Licensed User
I tested your code and tried even a "do while" loop.
Its a strange behaviour! :confused:
Sometimes it seems to work fine, sometimes the application seems to hang, even on the desktop just executing a "do nothing loop" with only 40 repeats.
 

RandomCoder

Well-Known Member
Licensed User
I think that perhaps you need to have a variable inside the For Next loop that is examined each time around the loop. If the error is caught then the variable can be set and this in turn causes the loop to be broken, personally I would use the Break command to exit the loop.

Regards,
RandomCoder
 

Cableguy

Expert
Licensed User
Thanks for your help,It seems the problem is solved...in my sub I had an open conection to a file and when the error was trapped the file wasn't closed causing the endless loop...Now it works fine
 
Top