Proper use of Goto and Resume

GT500Shlby

Newcomer
Joined
Jul 7, 2003
Messages
13
Location
New Jersey
I am having a problem with an ErrorHandler
With the Goto and Resumes. I want to call a Goto and then resume where I left off.

Code:
    Public Function errHandler(ByVal intErrFlag As Integer, Optional ByVal strErrString As String = "") As Integer

        Dim strErrorMessage As String
        Dim msgbxStyle As MessageBoxOptions
        Dim strErrorTitle As String
        Dim response As MsgBoxResult


        Select Case intErrFlag

            Case 1
                strErrorMessage = "Configuration and Data Files not found! Would you like to browse for them?"
                msgbxStyle = MsgBoxStyle.YesNo
                strErrorTitle = "Config File Error..."

                GoTo showbox
                Resume

                If response = MsgBoxResult.Yes Then
                    With OpenFileDialog1
                        .InitialDirectory = strConfigFile
                        .Filter = "PD Rating Configuration (*.ini)|*.ini"
                        .ShowDialog()
                        strConfigFile = .FileName()
                    End With

                    Return 1

                Else
                    strErrorMessage = "Cannot Recover from errors... Quitting."
                    msgbxStyle = MsgBoxStyle.OKOnly
                    strErrorTitle = "Quitting..."
                    End
                End If

                Exit Function

            Case 2

                strErrorMessage = "File Read Error: " & strErrString
                msgbxStyle = MsgBoxStyle.OKOnly
                strErrorTitle = "Error Reading File..."

                GoTo showbox
                Resume

                writErrorLog(2, strErrorMessage)
                Err.Clear()

                Exit Function


            Case 3

                strErrorMessage = "File Write Error: " & strErrString
                msgbxStyle = MsgBoxStyle.OKOnly
                strErrorTitle = "Error Writing File..."

                GoTo showbox
                Resume

                writErrorLog(2, strErrorMessage)
                Err.Clear()

                Exit Function
        End Select

        Exit Function


showbox:
        Beep()
        response = MsgBox(strErrorMessage, msgbxStyle, strErrorTitle)
        'Resume Next

    End Function
 
so like:
Code:
try
   'some code goes here

catch someError as Exception
  'do soemthing else
end try

But within the catch how would I test to see if the file exists or not, it seperates the catch and try's so a variale in try it not defined in the catch.

I have my filestream like this:

Code:
Dim fs2 As New FileStream(strConfigFile, FileMode.OpenOrCreate, FileAccess.Read)
            Dim tr As TextReader = New StreamReader(fs2)
            Dim kmfdm As String

I like having the Error handler seperate from the rest of the function. I really just want to know if I can call a Goto and then return exactly the next line after the goto. Without havign to define a billion labels because it is in an if-then statement, so the return position would be different every time.
 
try this , it will check if the file exsists then allow you to do your stuff.
Visual Basic:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f As IO.File

        Select Case f.Exists("C:\somefile.txt")
            Case True
                Dim sReader As IO.StreamReader = New IO.StreamReader(New IO.FileStream("C:\somefile.txt", IO.FileMode.Open))
                While Not sReader.Peek
                    MsgBox(sReader.ReadLine)
                End While
            Case False
                MessageBox.Show("oops the file dont exsist!")
        End Select
        
    End Sub
 
Is there a reason why you'd use a Select Case block there instead of a regular if...else construct?
 
you could also do this , without any Select Case / If statements :
Visual Basic:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim sReader As IO.StreamReader = New IO.StreamReader(New IO.FileStream("C:\somefile1.txt", IO.FileMode.Open))
            While Not sReader.Peek
                MessageBox.Show(sReader.ReadLine)
            End While
        Catch ex As Exception
            MessageBox.Show(ex.Message) '/// if it dont exsist , tell you about it.
        End Try
    End Sub

i'm sort of stuck in the Select Case routine personally , not sure what the exact advantages / disadvantages are over that and "If / Else If" , but in a True / False situation i find Select Case best ( case true , case false seems logical to me :) )
anyhow the ways above should help to determine if the file's there or not :)
 
Well I don't want to really display the error, my main goal is to warn them the file doesn't exist then popup the openfiledialog to look for the file.

But my main question was the proper usage of the GoTo. But I think I have found a way around that. Actually to add to my question;
how would I query the Windows Registry for a file name and keep the config file location there, and if it doesnt exist or the registry entry doesnt exist to pop-up the open file dialog and then find the file and create or modify the registry entry?
 
GT500Shlby said:
Well I don't want to really display the error, my main goal is to warn them the file doesn't exist then popup the openfiledialog to look for the file.

You dont need error trapping for that, simple use the IO.File.Exist method and if the file doesnt exist show the OpenFIleDialog.


But my main question was the proper usage of the GoTo. But I think I have found a way around that. Actually to add to my question;
how would I query the Windows Registry for a file name and keep the config file location there, and if it doesnt exist or the registry entry doesnt exist to pop-up the open file dialog and then find the file and create or modify the registry entry?

Look into the Microsoft.Win32.RegistryKey class for working with registry.
 
Okay, I got it. Thanks for your help.

And I guess VB.NET doesn't have a way to call a GoTo function and then return to the exact line right after where you called the Goto. It probably is not needed, but would be a lazy way to use GoTo instead of writing functions.
 
You will hate gotos.

Either you'll start hating them after the internal QA team made you redesign all your code ... or you'll start hating them when in 3 years you decide to add one special new functionality to your program. :)
 
Very True. Thats why I wanted to have the external error handler. I really do not wan't to handle the errors inside the programs code, except for a few safety checks. My main goal is to not really display errors, but either automatically fix them or alert the user to help in fixing them. I'm new to VB.NET, I had tinkered with VB6, but I was mostly a C++ person. However I was "pushed" into this project.
 
.Net does not compile to native/assembly code, it compiles to MSIL which is converted to Native/Assembly at runtime increasing runtime application performance.

On Error are slower than Try..Catch
Visual Basic:
If IO.File.Exists(Filename) = False Then Exit Sub

Try
     Dim sReader As IO.StreamReader = New IO.StreamReader(FileName)
            While Not sReader.Peek
                MessageBox.Show(sReader.ReadLine)
            End While
            sReader.Close
Catch IOerr as IO.IOException
     'Unable to read file (File in use, corrupted, etc)
     'Could start a loop to wait until its free
Catch exc as Exception
     'Unknown Error (probably uncompensateable error)
End Try
You can use Managed Code in C++ .Net Apps, if you like Managed only you can use C#
 
Last edited:
Back
Top