Question Unhandled Exception Error on Loading

garriew

Member
Joined
Sep 13, 2013
Messages
13
Programming Experience
Beginner
When my program loads, it's suppose to look for a settings file and if the file isn't found it's suppose to go ahead and load but use hardcoded variables. Then the file is created on exit.

In testing it works but after I compile it, I get the error an Unhandled Exception Error. It says:

System.IO.FileNotFoundException: Could not find file 'C:\Users\main\Desktop\settings.ini'.

If I click continue it loads the default settings load like they should.

My code is:

(This is done before anything and is in the public)

VB.NET:
Dim fileSettings As String = "C:\Users\main\Desktop\BingRewardsSearchBot.ini"

My onload has:

VB.NET:
        Dim ini As New IniFile(fileSettings)
        Dim MyFile As String = fileSettings
        Dim myFileLines() As String = IO.File.ReadAllLines(fileSettings)
        Dim myListOfWords As New List(Of String)
        'load settings
        If System.IO.File.Exists(MyFile) = True Then
            Dim delay = ini.ReadValue("settings", "delay")
            SearchDelay.Text = delay
            Dim search = ini.ReadValue("settings", "search")
            NoSearches.Text = search
            For Each strWordLine As String In myFileLines.Where(Function(x) x.StartsWith("Word"))
                myListOfWords.Add(strWordLine.Split("="c).Last)
            Next
            SearchWords.Text = String.Join(Environment.NewLine, myListOfWords.ToArray)
        Else
            SearchDelay.Text = "7-12"
            NoSearches.Text = "32-35"
        End If
 
Sure im glad it worked, it basically tells the debuger to ignore errors and allows your application to continue running, another alternative to this is disabling JIT debugging which i also think is very annoying.
 
what about adding this:
On Error Resume Next

tell me what happens and ill look more into it :)

That is the worst solution in the history of mankind I'm afraid. You should NEVER use On Error Resume Next in VB.NET. You don't just ignore errors. You find out what's causing them and you fix them.

I would question why you're using an INI file at all. That's another thing that shouldn't be used in VB.NET. This thread feels like it's from a VB6 forum. You should be using Application Settings.
 
well i think it solves it in this case cause the error was that it didnt find its settings file, this would be solved on app exit he asid so i guess it works in this occasion but thanks i wont use it for other stuff.
 
well i think it solves it in this case cause the error was that it didnt find its settings file, this would be solved on app exit he asid so i guess it works in this occasion but thanks i wont use it for other stuff.

It's never a good idea under any circumstances... period! If there's no file then why is the code looking for it in the first place? If it should be there then why isn't it? If it makes sense to look for the file but it also makes sense that it might not be there then the code should be calling File.Exists first. Using On Error Resume Next is just plain bad programming, here or anywhere else. It doesn't solve anything.
 
Well i guess he could just add that i didnt know, I have only been in vb.net for 6 months and i only knew vbscript before.
 
Well i guess he could just add that i didnt know, I have only been in vb.net for 6 months and i only knew vbscript before.
Fair enough, but ignoring errors without trying to find the root cause and prevent the error if possible is poor practice regardless of the language. If you do that analysis and conclude that On Error Resume Next is your only option then that's fine but On Error Resume Next is never your only option in VB.NET and should never be any option at all in VB.NET. It's there more to support upgraded VB6 code than anything else. If there's a reasonable way to prevent an exception being thrown then you implement it and, if there's not, then you use an appropriate Try...Catch block.
 
Back
Top