From the following two code segments, which one do you think is more
elegant?
******* CODE BLOCK #1 ***********
Dim strArrayFileTryList(5) as String
strArrayFileTryList(0) = "file1.txt"
strArrayFileTryList(1) = "file2.txt"
strArrayFileTryList(2) = "file3.txt"
strArrayFileTryList(3) = "file4.txt"
strArrayFileTryList(4) = "file5.txt"
Dim intCurFileTryArray as Integer
Try
SomethingToDoWithFile(strArrayFileTryList(intCurFileTryArray))
Catch e as Exception1
intCurFileTryArray += 1
If intCurFileTryArray > strArrayFileTryList.Length Then
MessageBox.Show ("Cannot open any of the files")
Else
ReTry
End If
Catch
End Try
******* END CODE BLOCK #1 ***********
******* CODE BLOCK #2 ***********
Dim strArrayFileTryList(5) as String
strArrayFileTryList(0) = "file1.txt"
strArrayFileTryList(1) = "file2.txt"
strArrayFileTryList(2) = "file3.txt"
strArrayFileTryList(3) = "file4.txt"
strArrayFileTryList(4) = "file5.txt"
Dim intCurFileTryArray as Integer
ReStart:
Try
SomethingToDoWithFile(strArrayFileTryList(intCurFileTryArray))
Catch e as Exception1
intCurFileTryArray += 1
If intCurFileTryArray > strArrayFileTryList.Length Then
MessageBox.Show ("Cannot open any of the files")
Else
Goto ReStart:
End If
Catch
End Try
******* END CODE BLOCK #2 ***********
Besides, Goto statement in this example appear to be "segmented" when
reading the flow of the code. IMO, it makes more sense to "ReTry" then ask
to "go somewhere". This is especially true when you have enormous
Try...Catch segements in one procedure because if we use GoTo statement, we
have to label them "ReTry1", "ReTry2" or "ReTry<<TaskName>>" which I think
distract the programmer when he/she reads the flow of the code. What do you
think??
--
Nay Myo Aung
Chief Visual Software Architect
MCP MCSD MCDBA
Email: owN0SPAMner @naymyoauN0SPAMng.name [remove NOSPAM s]
Homepage:
http://hyperdisc.unitec.ac.nz/postgrad/aungn01/