try和catch成对使用,catch用来捕获异常,这是众所周知的,原来以为catch后面不支持返回值呢,今天果断试了一下,竟然支持哦,其实细想就是一句话概括try {}catch{},就是只要try{}内发生异常就会执行catch{}内的内容",所以显然可以写返回值哦。哈哈,特别摘取项目中的一段代码:
try
{
for (i = 0; i < rows; i++)
{
string str = ds.Tables[0].Rows[i][0].ToString().Trim();
if (str != "")
{
Int64.Parse(ds.Tables[0].Rows[i][0].ToString());
Int64.Parse(ds.Tables[0].Rows[i][2].ToString());
Int64.Parse(ds.Tables[0].Rows[i][3].ToString());
Int64.Parse(ds.Tables[0].Rows[i][4].ToString());
Int64.Parse(ds.Tables[0].Rows[i][5].ToString());
Int64.Parse(ds.Tables[0].Rows[i][6].ToString());
}
}
return 0;
}
catch
{ MessageBox.Show(pathexcel + "格式有问题", "错误提示"); return 1; }
把他放在函数体里,就是根据判断返回值来判断执行什么操作。哈哈 你明白了!