Solved

Re-Try

Posted on 2006-06-27
6
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

626 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