我的form_load方法中有此代码...
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?");
}
}
我希望能够忽略正在读取的文件中的空行,因此我添加了该line != ""
位并在if语句中对其进行检查之前对其进行了修整。加载应用程序时,服务器列表将为空。因此,我切换到调试模式并进入此代码。当line
为空时,在if语句上按F11键,调试/步进将停止,并显示应用程序。我期望发生的事情是回到while循环,但是那没有发生。
我添加了另一个,并带有一个消息框作为测试...消息框不显示!
简而言之,当line
为空时,不会执行true和false的代码,调试器将停止单步执行该代码。
这里发生了什么事?我是否缺少明显的东西,或者这是Visual Studio中的错误?
这是Load
事件的问题。它以这样的方式调用:使内部的异常被静默地吞下。
作为示例,您可能会在代码中出错,该行:
if (line[0] != '#' && line != "")
如果line
变量包含一个空字符串,将抛出异常,因为这line[0]
是不正确的,因此空字符串没有索引#0。
但是,由于您是在Load
事件中执行此操作的,因此此类异常只会被吞下。
要解决此问题,请try/catch
在Load
事件处理程序中的所有代码周围添加一个块:
private void Form_Load(...)
{
try
{
... all your existing code here
}
catch (Exception ex) // add more specific exception handlers
{
... handle the exception here
}
}
以下是有关此问题的堆栈溢出中的其他一些问题和解答:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句