Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Re-Try

Posted on 2006-06-27
6
Medium Priority
?
204 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 1200 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 200 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 200 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

971 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