Re: On Local Error Goto Somewhere
- From: "R. MacDonald" <scitec@xxxxxxxxxxxxxxx>
- Date: Mon, 19 Jun 2006 10:33:38 +0200
Hello, Lorin,
IMO the GoTo statement has never been "THE" problem. The problem is the "ComeFrom" statement -- i.e. the label. When I am looking at a GoTo, I know immediately where the next statement is. When I see a label, I don't have a clue how I got there. You have said this yourself in your comment
' label are cheap, and I can search to
find the source of this destination
The point is, that you have to search, and searching (my opinion) is not cheap.
Cheers,
Randy
Lorin wrote:
This brings up the discussion of GOTO since On Error GOTO is a GOTO..
This is the VB6 code I use on occassion if pressed
Public Function MySub(sMyFile as String) as String()
ON ERROR GOTO MySubErr Dim lFNbr as long
Dims sBuffer as String
Dim sArgs() as String
Dim iX as Integer
sBuffer = String$(50," ")
' some code
Do
100 Open sMyFile For Binary As lFnbr
200 Get lFNbr,, sBuffer ' what happens if there are not that many characters in the file?
If Split(sBuffer,",")(0) = "VALID" Then ' just making a point
sArgs=Split(sBuffer, ",") ' now get all
For iX = 0 to UBound(sArgs)
if sArg(iX) = "OOPS" then Exit Do ' a convieient was to escape using this GOTO
' Note it skips out of the FOR as well as the DO
if sArg(iX) = "GOSH" Then GoTo FlyingLeap ' another type of GOTO
' Now which is easier to follow?
' Can your eye find the label first or the end of the Loop?
' Labels are automatically left justified.
' what if this was in a complex multilayered nested bit of code?
Next iX
MySub = sArgs
EndIf
Loop While 0 ' solely the target of a GOTO, can you spot it?
' mixed message, I know, but what the heck.
FlyingLeap: ' label are cheap, and I can search to find the source of this destination
' some additional code goes here to do something very usefull
MySubExit:
Close lFnbr ' it always closes due to controlled exiting
' cleanup even from errors
Exit Sub
MySubErr:
Debug.Print Err.Description
Select case ERL
Case 100
' do something here in code
' if there is a possibility on an error at this point then call a subroutine with its own error handling to do the work and catch an error
Case 200
' do something else here in code
Case Else
' handle other errors here
End Select
' all errors resume to MySubExit
Resume MySubExit
End Sub ' MySub <---- do you know why I always do this?
I know, all you purists go HISSSS and BOOOO.
THis is not Spaghetti, just clear and responsible coding.
Any discussion????
Stirring the pot.
"Lorin" wrote:
Please compare and contrast this with
On Error Goto Somewhere
Big thnks!
- Follow-Ups:
- Re: On Local Error Goto Somewhere
- From: Lorin
- Re: On Local Error Goto Somewhere
- References:
- RE: On Local Error Goto Somewhere
- From: Lorin
- RE: On Local Error Goto Somewhere
- Prev by Date: Re: Prevent fireing in a Leave Event
- Next by Date: Re: Picture.Width
- Previous by thread: Re: On Local Error Goto Somewhere
- Next by thread: Re: On Local Error Goto Somewhere
- Index(es):
Relevant Pages
|