GT500Shlby Posted July 7, 2003 Posted July 7, 2003 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 Quote
*Experts* mutant Posted July 7, 2003 *Experts* Posted July 7, 2003 Try..Catch block is so much better then that, I suggest you look into that kind of error handling. Quote
GT500Shlby Posted July 8, 2003 Author Posted July 8, 2003 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. Quote
Leaders dynamic_sysop Posted July 8, 2003 Leaders Posted July 8, 2003 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 Quote
*Gurus* divil Posted July 8, 2003 *Gurus* Posted July 8, 2003 Is there a reason why you'd use a Select Case block there instead of a regular if...else construct? Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Leaders dynamic_sysop Posted July 8, 2003 Leaders Posted July 8, 2003 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 :) Quote
GT500Shlby Posted July 8, 2003 Author Posted July 8, 2003 That's probably a matter of personal preference. I don't belive there is much of a difference when it gets translated to assembly. Quote
GT500Shlby Posted July 8, 2003 Author Posted July 8, 2003 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? Quote
*Experts* mutant Posted July 8, 2003 *Experts* Posted July 8, 2003 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. Quote
GT500Shlby Posted July 8, 2003 Author Posted July 8, 2003 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. Quote
Heiko Posted July 8, 2003 Posted July 8, 2003 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. :) Quote .nerd
GT500Shlby Posted July 8, 2003 Author Posted July 8, 2003 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. Quote
AndreRyan Posted July 10, 2003 Posted July 10, 2003 (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 July 10, 2003 by AndreRyan Quote .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?
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.