Solved

Re-Try

Posted on 2006-06-27
6
199 Views
Last Modified: 2011-09-20
vb2005

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)

thanks
0
Comment
Question by:newyuppie
6 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 300 total points
ID: 16997355
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.

Bob
0
 
LVL 14

Assisted Solution

by:ptakja
ptakja earned 50 total points
ID: 16998219
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.
0
 
LVL 9

Assisted Solution

by:lojk
lojk earned 50 total points
ID: 17000330
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.

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/dv_fxdesignguide/html/70b909de-5609-44ba-8a50-a46b4e0cd7d7.htm

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)
try
tflag=true'allow it to pass the loop unless fail
DoYourStuffHere
catch ex as exception
tFlagCounter=+1
tflag=false
end try
loop

interestingly, i wasnt aware until now but you can still use the old syntax in VS2005
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vbcn/html/e897d7ca-07e8-45dd-8a6d-a5b2a2fc9b9a.htm

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

hth
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 9

Expert Comment

by:lojk
ID: 17000336
sorry learnedone, spotted after posting that this was roughly what you said, saved you having to type the example though? :-)
0
 
LVL 13

Author Comment

by:newyuppie
ID: 17000654
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.

thanks
0
 
LVL 9

Expert Comment

by:lojk
ID: 17000919
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.



0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question