有时候运行结果错误,但是vs没抛异常,这时可以用trycatch来帮我们捕捉异常。
例如:bug的情况是treeview只显示一个根节点和一个子节点,还不报错,我擦~
private void f_script_Load(object sender, EventArgs e)
{
List<t_scripts> parents = new t_scriptsBLL().getByParentId(0) as List<t_scripts>;
try
{
foreach (t_scripts p in parents)
{
//TreeNode tn = new TreeNode(p.name);
TreeNode tn = new TreeNode();
tn.Text = p.name;
treeView1.Nodes.Add(tn);
addChildrenNodes(tn, (int)p.id);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
原来是“未将对象引用设置到对象的实例”。
下面我找到那个很二的bug了
//List<string> sum = new List<string>();//不报错
List<string> sum = null;//报错
foreach (string s in sum)
{
Console.WriteLine("??");
}
Console.WriteLine("!");
Console.ReadKey();
list如果未赋值是count=0的list,而不是null;foreach不能对null遍历。
ok,睡觉