Solved

Re-Try

Posted on 2006-06-27
6
201 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
Technology Partners: 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 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