結構化異常處理(二):配置異常的狀態

來源:互聯網
上載者:User

標籤:style   class   blog   code   http   tar   

一、TargetSite屬性(public MethodBase TargetSite { get; })

System.Exception.TargetSite屬性協助我們瞭解引發某個異常的方法的各種資訊。輸出TargetSite的值將顯示傳回值類型、方法名稱、引發異常方法的參數。

它不是只返回字串,而是返回一個強型別的System.Reflection.MethodBase對象。

1  Console.WriteLine("Member name: {0}", e.TargetSite);//輸出結果:Void Accelerate(Int32)2  Console.WriteLine("Class defining member: {0}", e.TargetSite.DeclaringType);//輸出結果:ConsoleApplication3.Car3  Console.WriteLine("Member Type: {0}", e.TargetSite.MemberType);//輸出結果:Method


說明:

1.我們使用MethodBase.DeclaringType屬性值來制定引發異常的類的全稱(這個例子中時ConsoleApplication3.Car)。

2.使用MethodBase對象的MemberType屬性來確定引發異常的成員類型(比如屬性和方法)。

二、StackTrace屬性(public virtual string StackTrace { get; })

System.Exception.StackTrace屬性協助我們標識引發異常的一些列調用。需要注意的是,StackTrace的值是異常建立時自動產生的,無法為其賦值。

例:

1 catch(Exception e)2 {3     Console.WriteLine("Stack: {0}", e.StackTrace);4 }

輸出:
Stack: 在 ConsoleApplication3.Car.Accelerate(Int32 delta) 位置 e:\Test\ConsoleApplication3\ConsoleApplication3\Program.cs:行號 66標識這個序列的首次調用
          在 ConsoleApplication3.Program.Main(String[] args) 位置 e:\Test\ConsoleApplication3\ConsoleApplication3\Program.cs:行號 18標識出錯成員的具體位置

在調試裝或記錄製定應用程式時,這些資訊都非常有用,它使我們能順其自然地發現錯誤的根源。

三、HelpLink屬性(public virtual string HelpLink{ get; set; })

預設情況下HelpLink屬性的值是一個Null 字元串。如果讀者需要一個有意義的值填充該屬性,就要在引發System.Exception類型異常之前賦值。

例:

將上例中的Accelerate方法更新

 1 public void Accelerate(int delta) 2         { 3             if (casIsDead) 4             { 5                 Console.WriteLine("{0} is out of order...", PetName); 6             } 7             else 8             { 9                 CurrentSpeed += delta;10                 if (CurrentSpeed >= MaxSpeed)11                 {12                     casIsDead = true;13                     CurrentSpeed = 0;14 15                     //我們需要調用HelpLink屬性,因此需要在異常對象引發之前先建立一個本地變數16                     Exception ex = new Exception(string.Format("{0} has overheated!", PetName));17                     ex.HelpLink = "http://www.CarsRus.com";18                     throw ex;19                 }20                 else21                 {22                     Console.WriteLine("=> CurrentSpeed = {0}", CurrentSpeed);23                 }24             }25         }

四、Data屬性(public virtual IDictionary Data { get; })
System.Exception中的Data屬性允許我們使用使用者提供的響應資訊來填充異常對象。

更新Accelerate方法:

 1 public void Accelerate(int delta) 2         { 3             if (casIsDead) 4             { 5                 Console.WriteLine("{0} is out of order...", PetName); 6             } 7             else 8             { 9                 CurrentSpeed += delta;10                 if (CurrentSpeed >= MaxSpeed)11                 {12                     casIsDead = true;13                     CurrentSpeed = 0;14 15                     //我們需要調用HelpLink屬性,因此需要在異常對象引發之前先建立一個本地變數16                     Exception ex = new Exception(string.Format("{0} has overheated!", PetName));17                     ex.HelpLink = "http://www.CarsRus.com";18 19                     //填充關於錯誤的自訂資料20                     ex.Data.Add("TimeStamp", string.Format("The car exploded at {0}", DateTime.Now));21                     ex.Data.Add("Cause", "You have a lead foot.");22                     throw ex;23                 }24                 else25                 {26                     Console.WriteLine("=> CurrentSpeed = {0}", CurrentSpeed);27                 }28             }

調用:

 1  static void Main(string[] args) 2         { 3             Car myCar = new Car("Zippy", 20); 4  5             try 6             { 7                 for (int i = 0; i < 10; i++) 8                 { 9                     myCar.Accelerate(10);10                 }11             }12             catch (Exception e)13             {14                 //預設情況下,data欄位是空的,需要檢查是否為空白15                 if (e.Data != null)16                 {17                     foreach (DictionaryEntry item in e.Data)18                     {19                         Console.WriteLine("-> {0}: {1}", item.Key, item.Value);20                     }21                 }               22             }23         }

輸出:

=> CurrentSpeed = 30
=> CurrentSpeed = 40
=> CurrentSpeed = 50
=> CurrentSpeed = 60
=> CurrentSpeed = 70
=> CurrentSpeed = 80
=> CurrentSpeed = 90
-> TimeStamp: The car exploded at 2014-06-26 16:16:54
-> Cause: You have a lead foot.

說明:

Data屬性非常有用,因為它允許我們打包關於“錯誤”的自訂資訊,無須構建全新的類類型來擴充Exception基類。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.