1

I have this code in my form_load method...

System.IO.StreamReader file =  new System.IO.StreamReader(serverList);
Servers = new List<Server>();
String line;
//while ((line = file.ReadLine()) != null)
while (! file.EndOfStream)
{
    line = file.ReadLine().Trim();
    if (line[0] != '#' && line != "")
    {
        Servers.Add(new Server() { ServerName = line.Split('|')[0], 
            IPorHostname = line.Split('|')[1] });
    }
    else
    {
        MessageBox.Show("I don't understand what the debugger is doing!  Is this a bug?");
    }
}

I wanted to be able to ignore empty lines in the file I was reading from, so I added the line != "" bit and trimmed the line before checking it in the if statement. When loading the application the server list would be empty. So I switched into debug mode and step into this code. When line is empty and I press F11 on the if statement, the debugging/stepping stops and the application shows. What I expect to happen is to go back around the while loop but that doesn't happen.

I added an else, with a message box as a test... The message box doesn't show!

In short, when line is empty neither the true nor false code is executed, and the debugger stops stepping through the code.

What's happening here? Am I missing something obvious or is this a bug in Visual Studio?

MrVimes
  • 3,212
  • 10
  • 39
  • 57

1 Answers1

3

This is a problem with the Load event. It is called in such a way that exceptions inside are swallowed silently.

As an example of what can go wrong in your code, this line:

if (line[0] != '#' && line != "")

will throw an exception if the line variable contains an empty string, since then line[0] is incorrect, there is no index #0 for an empty string.

However, since you're executing this in the Load event, such an exception is just swallowed.

To fix this, add a try/catch block inside the Load event handler, around all the code in there:

private void Form_Load(...)
{
    try
    {
        ... all your existing code here
    }
    catch (Exception ex) // add more specific exception handlers
    {
        ... handle the exception here
    }
}

Here are some other questions and answers here on Stack Overflow about this problem:

Community
  • 1
  • 1
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • I just swapped the conditions `line != "" && line[0] != '#'` and my code worked as expected. Your answer is correct and makes sense. (This was baffling me) I will accept your answer in 8 minutes when it will allow me to :) – MrVimes May 08 '16 at 20:08
  • Yes, that fixes the exception, but it does not fix your other problem, that exceptions thrown in Form_Load are silently swallowed. You should add a try/catch block around that method, or preferrably, find a different event to place your code inside. – Lasse V. Karlsen May 08 '16 at 20:08
  • I'll add code to handle exceptions, but for now, swapping the conditions makes my app run, and more to the point I now understand what was happening. – MrVimes May 08 '16 at 20:11