473,413 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

How to try something repeatedly using try-catch?

Hi,
In vb6, if I had an error handler I could catch an error,
make a correction and then try it again using resume. I
want to do the same thing in vb.net.

I am trying to deal with some concurrency issues. I want
to be able to catch a error, wait a few milliseconds and
then try it again. Any help on this would be great.
Thanks ... Ed

Nov 20 '05 #1
9 11427
Do you want to try again once? X times? Keep trying forever?

"Ed Staffin" <an*******@discussions.microsoft.com> wrote in message
news:b1****************************@phx.gbl...
Hi,
In vb6, if I had an error handler I could catch an error,
make a correction and then try it again using resume. I
want to do the same thing in vb.net.

I am trying to deal with some concurrency issues. I want
to be able to catch a error, wait a few milliseconds and
then try it again. Any help on this would be great.
Thanks ... Ed

Nov 20 '05 #2
Perhaps with some embellishment, something like this may work:

Dim blnSuccess As Boolean

Do Until blnSuccess
Try
... 'Whatever needs to be done
blnSuccess = True
Catch e as exception
.... If it is the error you think you can retry then wait
End Try
Loop
--
Al Reid

"It ain't what you don't know that gets you into trouble. It's what you know
for sure that just ain't so." --- Mark Twain

"Ed Staffin" <an*******@discussions.microsoft.com> wrote in message news:b1****************************@phx.gbl...
Hi,
In vb6, if I had an error handler I could catch an error,
make a correction and then try it again using resume. I
want to do the same thing in vb.net.

I am trying to deal with some concurrency issues. I want
to be able to catch a error, wait a few milliseconds and
then try it again. Any help on this would be great.
Thanks ... Ed

Nov 20 '05 #3
Ed,
In addition to the Loop that Al showed, you can use Goto within a Catch
block to go to a label in the Try Block.

In this instance I consider Goto more of a Resume Try than an actual Goto.
;-)

Try
TryAgain:
... 'Whatever needs to be done
Catch e as exception
.... If it is the error you think you can retry then wait
Goto TryAgain
End Try

Normally I put an If around the "Resume Try" (goto) so as to avoid an
endless loop.

Something like:
Try
TryAgain:
AttemptToReadFile()
' Throw New System.IO.FileNotFoundException
Catch ex As System.IO.FileNotFoundException
If MessageBox.Show("What would you like to do?", "File not
found", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2) = DialogResult.Retry Then
GoTo TryAgain
Else
Throw ' let error propagate up
End If
End Try

Hope this helps
Jay

"Ed Staffin" <an*******@discussions.microsoft.com> wrote in message
news:b1****************************@phx.gbl...
Hi,
In vb6, if I had an error handler I could catch an error,
make a correction and then try it again using resume. I
want to do the same thing in vb.net.

I am trying to deal with some concurrency issues. I want
to be able to catch a error, wait a few milliseconds and
then try it again. Any help on this would be great.
Thanks ... Ed

Nov 20 '05 #4
Jay,

Somehow, the use of the "GoTo" statement just doesn't ever seem to come to mind as a solution unless I am *really* stuck and can
find no other way out. For others, it seems to be the first and only choice.

To each his own.

--
Al Reid

"It ain't what you don't know that gets you into trouble. It's what you know
for sure that just ain't so." --- Mark Twain

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:ue****************@TK2MSFTNGP09.phx.gbl...
Ed,
In addition to the Loop that Al showed, you can use Goto within a Catch
block to go to a label in the Try Block.

In this instance I consider Goto more of a Resume Try than an actual Goto.
;-)

Try
TryAgain:
... 'Whatever needs to be done
Catch e as exception
.... If it is the error you think you can retry then wait
Goto TryAgain
End Try

Normally I put an If around the "Resume Try" (goto) so as to avoid an
endless loop.

Something like:
Try
TryAgain:
AttemptToReadFile()
' Throw New System.IO.FileNotFoundException
Catch ex As System.IO.FileNotFoundException
If MessageBox.Show("What would you like to do?", "File not
found", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2) = DialogResult.Retry Then
GoTo TryAgain
Else
Throw ' let error propagate up
End If
End Try

Hope this helps
Jay

"Ed Staffin" <an*******@discussions.microsoft.com> wrote in message
news:b1****************************@phx.gbl...
Hi,
In vb6, if I had an error handler I could catch an error,
make a correction and then try it again using resume. I
want to do the same thing in vb.net.

I am trying to deal with some concurrency issues. I want
to be able to catch a error, wait a few milliseconds and
then try it again. Any help on this would be great.
Thanks ... Ed


Nov 20 '05 #5
Something like this might work ok......

Dim bDone as boolean = false

Do
Try
.......
.......
.......

bDone = true

catch Ex as Exception

End Try

loop while not bDone

"Ed Staffin" <an*******@discussions.microsoft.com> wrote in message
news:b1****************************@phx.gbl...
Hi,
In vb6, if I had an error handler I could catch an error,
make a correction and then try it again using resume. I
want to do the same thing in vb.net.

I am trying to deal with some concurrency issues. I want
to be able to catch a error, wait a few milliseconds and
then try it again. Any help on this would be great.
Thanks ... Ed

Nov 20 '05 #6
Hi Ed,

I have nothing to add to that nice compact code from Al (or Robin), however
when you are doing that, did you also have already seen this rollback, when
you do not do something like this or in another way, I think that you will
get with a dataset strange behaviour.

http://support.microsoft.com/default...b;en-us;310351

Cor
Nov 20 '05 #7
This code will give you a specified number of attempts to handle an exception..

Dim retries As Long = 3 'number of attempts before rethrowing to an outer level
Tr
retry:
retries -=
'normal code to execut
Catch ex As Exceptio
If (retries >= 0)
'Rectify problem..
GoTo retr
Else 'rethrow the exact same exception..
Throw e
End I
End Try
Nov 20 '05 #8
On Tue, 11 May 2004 08:02:38 -0500, Jay B. Harlow [MVP - Outlook] wrote:
Ed,
In addition to the Loop that Al showed, you can use Goto within a Catch
block to go to a label in the Try Block.

Jay, is there any reason to put the label *inside* the Try instead of
outside it? Does it make any difference?

Try
TryAgain:
'Whatever
Catch
If SomeCondition Then
Goto TryAgain
EndIf
End Try
As opposed to this:

TryAgain:
Try
'Whatever
Catch
If SomeCondition Then
Goto TryAgain
EndIf
End Try
In this instance I consider Goto more of a Resume Try than an actual Goto.
;-)


I think adding a ReTry keyword would be nice, but like you said you'd have
to watch out for an endless loop. Perhaps the ReTry could have optional
parameters to prevent that:

ReTry [[MaxRetries][,RetryDelayMs]]

Just a thought
--
Chris

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 20 '05 #9
Al,
Unfortunately too many people feel that bad use of a Goto makes the Goto
bad.

I feel "correctness" is more important then any preconceived notions I may
have had about bad uses of Goto in the past.

Of course "correctness" is subjective.

Hope this helps
Jay

"Al Reid" <ar*****@reidDASHhome.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Jay,

Somehow, the use of the "GoTo" statement just doesn't ever seem to come to mind as a solution unless I am *really* stuck and can find no other way out. For others, it seems to be the first and only choice.
To each his own.

--
Al Reid

"It ain't what you don't know that gets you into trouble. It's what you know for sure that just ain't so." --- Mark Twain

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message

news:ue****************@TK2MSFTNGP09.phx.gbl...
Ed,
In addition to the Loop that Al showed, you can use Goto within a Catch
block to go to a label in the Try Block.

In this instance I consider Goto more of a Resume Try than an actual Goto. ;-)

Try
TryAgain:
... 'Whatever needs to be done
Catch e as exception
.... If it is the error you think you can retry then wait
Goto TryAgain
End Try

Normally I put an If around the "Resume Try" (goto) so as to avoid an
endless loop.

Something like:
Try
TryAgain:
AttemptToReadFile()
' Throw New System.IO.FileNotFoundException
Catch ex As System.IO.FileNotFoundException
If MessageBox.Show("What would you like to do?", "File not
found", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2) = DialogResult.Retry Then
GoTo TryAgain
Else
Throw ' let error propagate up
End If
End Try

Hope this helps
Jay

"Ed Staffin" <an*******@discussions.microsoft.com> wrote in message
news:b1****************************@phx.gbl...
Hi,
In vb6, if I had an error handler I could catch an error,
make a correction and then try it again using resume. I
want to do the same thing in vb.net.

I am trying to deal with some concurrency issues. I want
to be able to catch a error, wait a few milliseconds and
then try it again. Any help on this would be great.
Thanks ... Ed



Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text...
9
by: David Stockwell | last post by:
In referring to my copy of the python bible, it tells me I can't use all three items 'try' except and finally. I can use the t/f or t/e combinations though What combination can i use if i want...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
1
by: PaowZ | last post by:
Hi there! I encounter some troubles using $_SESSION, since I use a popup window in my php code. I mean, $_SESSION contains a certain value, when I click to get popup window opened, $_SESSION...
11
by: Nemanja Trifunovic | last post by:
A question for C# language experts: If I have a struct that implements IDisposable: struct C : IDisposable { public int clan; public void Dispose() { Console.WriteLine("Disposing");
0
by: Allan via DotNetMonster.com | last post by:
I am trying to save the xml doc by using asp.net, and i also know how to add the record. Dim myDS As New DataSet("Job Information") If System.IO.File.Exists(Server.MapPath("Hello.xml")) Then...
5
by: hangaround | last post by:
I wonder how would the CSharp avoid repeative include Assembly repeatively, because we coud using same Assembly in on just one Assembly twice or more.
67
by: Rui Maciel | last post by:
I've been delving into finite state machines and at this time it seems that the best way to implement them is through an intense use of the goto statement. Yet, everyone plus their granmother is...
3
by: Jeff | last post by:
..NET 2.0 Lets say I have this code: public int test () { string s = "hello world" try { int i = 4;
9
by: TomLasky | last post by:
Hi everyone, I'm an old school vb6 user trying to transition to VB.NET. Can anyone suggest a better approach to constantly checking something without using a timer? I'm trying to constantly...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.