1. 程式人生 > 實用技巧 >C# 使用throw;throw ex;和 throw new Exception();拋異常區別與建議

C# 使用throw;throw ex;和 throw new Exception();拋異常區別與建議

之前,在使用異常捕獲語句try...catch...throw語句時,一直沒太留意幾種用法的區別,前幾天除錯程式時發展找不到異常根源,無意中瞭解到幾種使用方法是有區別的。總結如下:

我們都知道,C#中使用throw和throw ex丟擲異常,但二者是有區別的。

在C#中推薦使用throw;來丟擲異常;throw ex;會將到現在為止的所有資訊清空,認為你catch到的異常已經被處理了,只不過處理過程中又丟擲新的異常,從而找不到真正的錯誤源。

throw new Exception()包裝一個異常,把內部異常Exception也丟擲,這樣丟擲的異常是最全面詳細的異常。

throw的用法主要有以下幾種:

第一種(不推薦使用,可惜很多人都一直這麼用的),這樣適用會吃掉原始異常點,重置堆疊中的異常起始點:

try
{
}
catch (Exception ex)
{
    throw ex;

第二種,可追溯到原始異常點,不過編譯器會警告,定義的ex未有使用:

try
{
}
catch (Exception ex)
{
    throw;

第三種,不帶異常引數的,這個同第二種其實一樣,可捕獲所有型別的異常,IDE不會告警:

try
{
}
catch 
{
    throw;
}

第四種:經過對異常重新包裝,但是會保留原始異常點資訊。推薦使用。

try
{
}
catch (Exception ex)
{
    
throw new Exception("經過進一步包裝的異常", ex); }

下面舉例測試:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace Api.Service.Common
 6 {
 7     public class ExceptionClass
 8     {
 9         /// <summary>
10         /// 丟擲異常方法
11         /// </summary>
12         public
void ExceptionThrow1() 13 { 14 try 15 { 16 // 呼叫原始異常丟擲方法來丟擲異常 17 this.ExceptionMethod(); 18 } 19 catch (Exception ex) 20 { 21 throw ex; 22 } 23 } 24 25 /// <summary> 26 /// 丟擲異常方法1 27 /// </summary> 28 public void ExceptionThrow2() 29 { 30 try 31 { 32 this.ExceptionMethod(); 33 } 34 catch (Exception ex) 35 { 36 throw; 37 } 38 } 39 40 /// <summary> 41 /// 丟擲異常方法2 42 /// </summary> 43 public void ExceptionThrow3() 44 { 45 try 46 { 47 this.ExceptionMethod(); 48 } 49 catch 50 { 51 throw; 52 } 53 } 54 55 /// <summary> 56 /// 丟擲異常方法3 57 /// </summary> 58 public void ExceptionThrow4() 59 { 60 try 61 { 62 this.ExceptionMethod(); 63 } 64 catch (Exception ex) 65 { 66 throw new Exception("經過進一步包裝的異常", ex); 67 } 68 } 69 70 /// <summary> 71 /// 原始異常丟擲方法 72 /// </summary> 73 private void ExceptionMethod() 74 { 75 throw new DivideByZeroException(); 76 } 77 } 78 }
       ExceptionClass exceptionClass = new ExceptionClass();
            try
            {
                exceptionClass.ExceptionThrow1();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.WriteLine("分割線--------------------------------------");
            try
            {
                exceptionClass.ExceptionThrow2();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.WriteLine("分割線--------------------------------------");
            try
            {
                exceptionClass.ExceptionThrow3();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.WriteLine("分割線--------------------------------------");
            try
            {
                exceptionClass.ExceptionThrow4();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

舉例結果:

我們可以明顯看出new Exception()自己包裝的異常比較詳細,能找到異常的跟蹤,其次是throw 都可以找到異常源。

throw ex;會把異常吞掉,丟擲新的異常,這樣會讓開發人員找不到異常源。

推薦使用new Excetion()也就是第四種方式。