C# 自定义异常的方法代码演示及说明

From , 5 Years ago, written in C#, viewed 166 times.
URL https://pastebin.vip/view/9713faa2
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.IO;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. namespace ConsoleApplication3
  9. {
  10. [Serializable] //声明为可序列化的 因为要写入文件中
  11. public class PayOverflowException : ApplicationException//由用户程序引发,用于派生自定义的异常类型
  12. {
  13. /// <summary>
  14. /// 默认构造函数
  15. /// </summary>
  16.         public PayOverflowException() { }
  17.         public PayOverflowException(string message)
  18.                 : base(message) { }
  19.         public PayOverflowException(string message, Exception inner)
  20.                 : base(message, inner) { }
  21. //public PayOverflowException(System.Runtime.Serialization.SerializationInfo info,
  22. // System.Runtime.Serialization.StreamingContext context)
  23. // : base(info, context) { }
  24. }
  25. internal class Employee
  26. {
  27.         public int ID { get; set; }
  28.         public string Name { get; set; }
  29. /// <summary>
  30. /// current pay
  31. /// </summary>
  32.         public int CurrPay { get; set; }
  33.         public Employee() { }
  34.         public Employee(int id, string name, int currpay)
  35.         {
  36.                 this.ID = id;
  37.                 this.Name = name;
  38.                 this.CurrPay = currpay;
  39.         }
  40. /// <summary>
  41. /// 定义一个GiveBunus的虚方法以供不同的派生类进行重载
  42. /// </summary>
  43. /// <param name="amount">奖金额度</param>
  44.         public virtual void GiveBunus(int amount)
  45.         {
  46. //用一个临时变量记录递增之前的值
  47.                 var pay = CurrPay;
  48.                 this.CurrPay += amount;
  49.                 if (CurrPay > 10000)
  50.                 {
  51. //发生异常,将CurrPay的值进行恢复,
  52. //并抛出异常,外部程序捕获次异常
  53.                         this.CurrPay = pay;
  54.                         var ex = new PayOverflowException("The employee's max pay should be no more than 10000.");
  55.                         throw ex;
  56.                 }
  57.         }
  58. }
  59. class Program
  60. {
  61.         static void Main(string[] args)
  62.         {
  63.                 Console.WriteLine("**** 创建Employee对象,并用try/catch捕获异常 *****");
  64.                 var emp = new Employee(10001, "Yilly", 8000);
  65.                 try
  66.                 {
  67.                         emp.GiveBunus(3000);
  68.                 }
  69.                 catch (PayOverflowException ex)
  70.                 {
  71.                         Console.WriteLine("异常信息:{0}\n发生于{1}类的{2}方法", ex.Message,
  72.                                           ex.TargetSite.DeclaringType, ex.TargetSite.Name);
  73.                         try
  74.                         {
  75.                                 var file = new FileStream(@"c:\customerexception.txt", FileMode.Create);
  76. //*** 异常信息写入文件中的代码省略...
  77. //以序列化方式写入
  78.                                 BinaryFormatter bf = new BinaryFormatter();
  79.                                 bf.Serialize(file, ex);
  80.                                 file.Close();
  81. //以字节方式写入
  82. //byte[] buffer = System.Text.Encoding.Default.GetBytes(ex.Message);
  83. //int leng = 0;
  84. //leng = buffer.GetLength(0);
  85. //file.Write(buffer, 0, leng);
  86. //file.Close();
  87.                         }
  88.                         catch (Exception ex1)
  89.                         {
  90.                                 var inner = new PayOverflowException(ex.Message, ex1);
  91.                                 throw inner;
  92.                         }
  93.                 }
  94.         }
  95. }
  96. }
  97.  
  98. //csharp/6013

Reply to "C# 自定义异常的方法代码演示及说明"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website