try catch 的新认识

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; }

 

 

把他放在函数体里,就是根据判断返回值来判断执行什么操作。哈哈 你明白了!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值