Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Try/Catch/Finally is easy to understand but converting the old GoTos from VB6 has been difficult for me when the GoTo target has been reachable from two or more places. Please show me how to handle this situation (which presumably is very simple once you have learnt it).

FileOpen(1, "AA.txt", OpenMode.Input)

        On Error GoTo EOFErr

        Me.Text = LineInput(1).Trim("""")
        Label1.Text = LineInput(1).Trim("""")

        On Error Resume Next ' no problem if files missing '

        Input(1, Linetxt)
        If Linetxt <> "none" Then Me.BackgroundImage = System.Drawing.Image.FromFile(Linetxt)
        Input(1, Linetxt)
        If Linetxt <> "none" Then Me.Picture1.Image = System.Drawing.Image.FromFile(Linetxt)

        If Not EOF(1) Then titleStr = LineInput(1).Trim("""")
        If Not EOF(1) Then subtitleStr = LineInput(1).Trim("""")

        On Error GoTo EOFErr

        Input(1, r)
        Input(1, s)
        Input(1, t)
        Input(1, u)

EOFErr:  ' no problem if anything is missing '
        FileClose(1)


I could perhaps put the whole thing in Try - End Try with an empty Catch but that would stop the program from reading the next line if some image file is missing.

What I have tried:

Try/Catch is not difficult if there is one GoTo leading to the target statement. But this is new for me. Old thinking does not go away and old code has to be updated to VB.net some day.
Posted
Updated 15-Nov-21 4:27am
Comments
CHill60 15-Nov-21 10:14am    
Never have an empty Catch statement! It's pointless
Jo_vb.net 15-Nov-21 10:28am    
Instead of empty Catch you can write errors to log file:

Catch ex As Exception
IO.File.AppendAllText(sAppPath & "\Log.txt", String.Format("{0}{1}", Environment.NewLine, Now.ToString & "; " & ex.ToString()))

1 solution

The VB6 error handling you have shared is not very good at all

FileOpen is a likely point of failure so should be surrounded by a Try-Catch. As you are reading lines from the file should validate that the data is or is not present and code accordingly. Using the raising of an exception by the system (e.g. File Not Found) is NOT a good programming technique.

You would be better off reading the entire file and then deciding what data you have and what to do with it. E.g. see Visual Basic .NET programming: Reading a Text File Line by Line[^]

More general advice on trying to convert error handling from VB6 code to VB.NET is ...don't. Use the VB6 code as a proof-of-concept, a template, a set of test results but you will be better off working out what it is trying to achieve and then writing that from scratch in VB.NET. If you do that, then this perceived problem of multiple source points for an on-error target simply disappears
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900