Solved

Re-Try

Posted on 2006-06-27
6
197 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now