Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

 

   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

Posted

so like:

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:

 

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.

  • Leaders
Posted

try this , it will check if the file exsists then allow you to do your stuff.

   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

  • Leaders
Posted

you could also do this , without any Select Case / If statements :

   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 :)

Posted

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?

  • *Experts*
Posted
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.

Posted

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.

Posted

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. :)

.nerd
Posted
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.
Posted (edited)

.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

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#

Edited by AndreRyan
.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...