i have some code within a try-catch block. how can i make the TRY part repeat itself calling it from the CATCH part? (if error, retry)

i want code for .net 2 framework. (in vb6 i used the GOTO statement, but if there's a way to not use old vb6 code id appreciate it, maybe theres a better way now)

LVL 13
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Bob LearnedConnect With a Mentor Commented:
Can you give me an example where this is necessary?  You can use a While loop with a condition that is met when no exception is thrown.

ptakjaConnect With a Mentor Commented:
You have to be careful when programming something like that. For example, variables declared inside the Try are not visible inside the Catch.  So you could easily end up with an infinite loop if you don't modifiy any data.

Typically, you either handle the exception in the code, or re-throw it and let the caller deal with it and make the decision on whether or not the subroutine gets executed again.
lojkConnect With a Mentor .Net and Infrastructure ConsultantCommented:
Agree with ptakja, not generally a good idea but I know what it feels like being a hardcore VB6 head and being thrust into .net.


Dim tFlag as boolean=false
Dim tFlagCounter as integer=0'make sure it doesnt go round too many times
do while (tFlag=false )and (tFlagCounter<50)
tflag=true'allow it to pass the loop unless fail
catch ex as exception
end try

interestingly, i wasnt aware until now but you can still use the old syntax in VS2005

So 'On Error Goto StartLoop' may still be valid after all (if a little frowned on)

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

lojk.Net and Infrastructure ConsultantCommented:
sorry learnedone, spotted after posting that this was roughly what you said, saved you having to type the example though? :-)
newyuppieAuthor Commented:
thanks for all your answers. instead of doing it the old vb6 way (on error goto) i decided to go for the loop LearnedOne and lojk talked about.
thanks also to ptakja for his warning.

i will do the following: raise the points and give LearnedOne most of the credit for giving the answer i ended up using first, and some credit to ptakja and lojk for expanding on the idea.

lojk.Net and Infrastructure ConsultantCommented:
thanks for the points and asker feedback, newyuppie... :-)

also just noticed my example was wrong (in case you'd copied it verbatim).. should say...

tFlagCounter+=1 'otherwise never *increases* the counter.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.