Chris,
Jay, is there any reason to put the label *inside* the Try instead of
outside it? Does it make any difference?
Yes there is a reason, it won't compile if its outside ;-)
I actually got this tip from Paul Vick's book "The Visual Basic .NET
Programming Language" from Addison Wesley. And yes he cautions, and I concur
that you should only use a Goto if its absolutely necessary.
Basically:
- A Goto can only branch from a Catch statement into the Try block of the
same statement
- A Goto can never branch out of a Finally statement
- A Goto can never branch into a Catch or Finally statement
He also lists that a Goto can cannot branch into With, For, For Each, or
SyncLock statements.
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]]
I would include an optional label also, as you can have different Labels in
the Try block with different Goto's. I'm not sure how often I would have
more then a single Goto, as its not as obvious as a single goto what is
going on.
Try
TryAgain1:
Something()
TryAgain2:
SomethingElse()
Catch
If whatever Then
Goto TryAgain1
Else
Goto TryAgain2
End If
End Try
Hope this helps
Jay
Chris Dunaway said:
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.