zoukankan      html  css  js  c++  java
  • throw和throw ex的区别

    C#中使用throw和throw ex抛出异常,但二者是有区别的。

    第一种(不推荐使用,可惜很多人都一直这么用的,包括俺,嘻嘻),这样适用会吃掉原始异常点,重置堆栈中的异常起始点:

    1             try
    2             {
    3             }
    4             catch (Exception ex)
    5             {
    6                 throw ex;
    7             }

    第二种,可追溯到原始异常点,不过编译器会警告,定义的ex未有使用:

    1             try
    2             {
    3             }
    4             catch (Exception ex)
    5             {
    6                 throw;
    7             }

    第三种,不带异常参数的,这个同第二种其实一样,可捕获所有类型的异常,IDE不会告警:

    1 try
    2             {
    3             }
    4             catch
    5             {
    6                 throw;
    7             }

    其实第二种和第三种用法,书上也是不建议使用的,一般要从小粒度的异常捕获开始,采用多个catch语句

    1            try
    2             {
    3             }
    4             catch (Exception ex)
    5             {
    6                 throw new Exception("经过进一步包装的异常", ex);
    7             }

    第四种:经过对异常重新包装,但是会保留原始异常点信息。推荐使用。

    1             try
    2             {
    3             }
    4             catch (Exception ex)
    5             {
    6                 throw new Exception("经过进一步包装的异常", ex);
    7             }

    例如:

      1 /// <summary>
      2         /// 入口方法
      3         /// </summary>
      4         public static void Main()
      5         {
      6             ExceptionClass ec = new ExceptionClass();
      7 
      8             try
      9             {
     10                 ec.ExceptionThrow1();
     11             }
     12             catch (Exception ex)
     13             {
     14                 Console.WriteLine(ex.ToString());
     15             }
     16 
     17             Console.WriteLine("---------------------------------------------------------------------");
     18 
     19             try
     20             {
     21                 ec.ExceptionThrow2();
     22             }
     23             catch (Exception ex)
     24             {
     25                 Console.WriteLine(ex.ToString());
     26             }
     27 
     28             Console.WriteLine("---------------------------------------------------------------------");
     29 
     30             try
     31             {
     32                 ec.ExceptionThrow3();
     33             }
     34             catch (Exception ex)
     35             {
     36                 Console.WriteLine(ex.ToString());
     37             }
     38 
     39             Console.WriteLine("---------------------------------------------------------------------");
     40 
     41             try
     42             {
     43                 ec.ExceptionThrow4();
     44             }
     45             catch (Exception ex)
     46             {
     47                 Console.WriteLine(ex.ToString());
     48             }
     49 
     50             Console.WriteLine("---------------------------------------------------------------------");
     51 
     52             Console.ReadKey();
     53         }
     54     }
     55 
     56     /// <summary>
     57     /// 该Class用来测试异常抛出时相关上下文栈的调用情况
     58     /// </summary>
     59     public class ExceptionClass
     60     {
     61         /// <summary>
     62         /// 抛出异常方法
     63         /// </summary>
     64         public void ExceptionThrow1()
     65         {
     66             try
     67             {
     68                 // 调用原始异常抛出方法来抛出异常
     69                 this.ExceptionMethod();
     70             }
     71             catch (Exception ex)
     72             {
     73                 throw ex;
     74             }
     75         }
     76 
     77         /// <summary>
     78         /// 抛出异常方法1
     79         /// </summary>
     80         public void ExceptionThrow2()
     81         {
     82             try
     83             {
     84                 this.ExceptionMethod();
     85             }
     86             catch (Exception ex)
     87             {
     88                 throw;
     89             }
     90         }
     91 
     92         /// <summary>
     93         /// 抛出异常方法2
     94         /// </summary>
     95         public void ExceptionThrow3()
     96         {
     97             try
     98             {
     99                 this.ExceptionMethod();
    100             }
    101             catch
    102             {
    103                 throw;
    104             }
    105         }
    106 
    107         /// <summary>
    108         /// 抛出异常方法3
    109         /// </summary>
    110         public void ExceptionThrow4()
    111         {
    112             try
    113             {
    114                 this.ExceptionMethod();
    115             }
    116             catch (Exception ex)
    117             {
    118                 throw new Exception("经过进一步包装的异常", ex);
    119             }
    120         }
    121 
    122         /// <summary>
    123         /// 原始异常抛出方法
    124         /// </summary>
    125         private void ExceptionMethod()
    126         {
    127             throw new DivideByZeroException();
    128         }
    129    }
  • 相关阅读:
    设置eclipse启动时所需要的jdk
    Mvc ModelState.isValid为false时,检查时那个字段不符合规则的代码
    360急速浏览器JS的调试
    Python3 安装第三方包
    Sqlserver生成带数据的脚本
    Spring(八)核心容器
    Spring(七)核心容器
    Spring(六)核心容器
    Spring(五)核心容器
    Spring(四)核心容器
  • 原文地址:https://www.cnblogs.com/jiebo/p/6683298.html
Copyright © 2011-2022 走看看