首页 > 其他 > 详细

知识点。

时间:2020-04-10 11:36:53      阅读:52      评论:0      收藏:0      [点我收藏+]

throw throw ex的区别 

      static void Main(string[] args)
        {
            Console.WriteLine(GetNumbertest(-1));
            Console.WriteLine(GetNum(-1));
            Console.ReadKey();
        }
static int GetNumbertest(int index)
        {
            int[] numbers = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };

            if (index < 0 || index >= numbers.Length)
            {
                throw new IndexOutOfRangeException();//调用执行后报错的位置指定是在这里
            }
return numbers[index];
}
 static int GetNumber(int index)
        {
            try
            {
                int[] numbers = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
                if (index < 0 || index >= numbers.Length)
                { 
return 0; } return numbers[index]; } catch(Exception ex) { throw ex;//调用执行后报错的位置指定是在这里 } }

从上面的的调用中可以看出来,throw 能够指定到具体的存在问题的代码位置,而throw ex 不能,你只能知道在这个 try,catch中存在逻辑或者调用出现了问题。

 

知识点。

原文:https://www.cnblogs.com/shanshuiYiCheng/p/12672216.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!