建立異常類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;函數

#region 概述
//在C#中全部的異常類型都繼承自System.Exception,也就是說,System.Exception是全部異常類的基類. 總起來講,其派生類分爲兩種:
//   1. SystemException類: 全部的CLR提供的異常類型都是由SystemException派生。
//   2. ApplicationException類: 由用戶程序引起,用於派生自定義的異常類型,通常不直接進行實例化。

//   建立自定義異常類應嚴格遵循幾個原則
//1. 聲明可序列化(用於進行系列化,固然若是你不須要序列化。那麼能夠不聲明爲可序列化的)
//2. 添加一個默認的構造函數
//3. 添加包含message的構造函數
//4. 添加一個包含message,及內部異常類型參數的構造函數
//5. 添加一個序列化信息相關參數的構造函數.
#endregion

namespace 建立異常類  
{  
    [Serializable] //聲明爲可序列化的 由於要寫入文件中  
    public class PayOverflowException : ApplicationException//由用戶程序引起,用於派生自定義的異常類型  
    {  
        /// <summary>  
        /// 默認構造函數  
        /// </summary>  
        public PayOverflowException() { }  
        public PayOverflowException(string message)  
            : base(message) { }  
        public PayOverflowException(string message, Exception inner)  
            : base(message, inner) { }  
        //public PayOverflowException(System.Runtime.Serialization.SerializationInfo info,  
        //    System.Runtime.Serialization.StreamingContext context)  
        //    : base(info, context) { }  
    }  

    internal class Employee  
    {  
        public int ID { get; set; }  
        public string Name { get; set; }  
        /// <summary>  
        /// current pay  
        /// </summary>  
        public int CurrPay { get; set; }  

        public Employee() { }  
        public Employee(int id, string name, int currpay)  
        {  
            this.ID = id;  
            this.Name = name;  
            this.CurrPay = currpay;  
        }  

        /// <summary>  
        /// 定義一個GiveBunus的虛方法以供不一樣的派生類進行重載  
        /// </summary>  
        /// <param name="amount">獎金額度</param>  
        public virtual void GiveBunus(int amount)  
        {  
            //用一個臨時變量記錄遞增以前的值  
            var pay = CurrPay;  

            this.CurrPay += amount;  

            if (CurrPay > 10000)  
            {  
                //發生異常,將CurrPay的值進行恢復,  
                //並拋出異常,外部程序捕獲次異常  
                this.CurrPay = pay;  
                var ex = new PayOverflowException("拋出異常了");  
                throw ex;  
            }  
        }  
    }  

    class Program  
    {  
        static void Main(string[] args)  
        {  
            Console.WriteLine("**** 建立Employee對象,並用try/catch捕獲異常 *****");  

            var emp = new Employee(10001, "Yilly", 8000);  
            try  
            {  
                emp.GiveBunus(3000);  
            }  
            catch (PayOverflowException ex)  
            {  
                Console.WriteLine("異常信息:{0}\n發生於{1}類的{2}方法", ex.Message,  
                    ex.TargetSite.DeclaringType, ex.TargetSite.Name);    
                try  
                {  
                    var file = new FileStream(@"c:\customerexception.txt", FileMode.Create);  
                    //*** 異常信息寫入文件中的代碼省略...  
                    //以序列化方式寫入  
                    BinaryFormatter bf = new BinaryFormatter();  
                    bf.Serialize(file, ex);  
                    file.Close();  

                    //以字節方式寫入  
                    //byte[] buffer = System.Text.Encoding.Default.GetBytes(ex.Message);  
                    //int leng = 0;  
                    //leng = buffer.GetLength(0);  
                    //file.Write(buffer, 0, leng);  
                    //file.Close();  
                }  
                catch (Exception ex1)  
                {  
                    var inner = new PayOverflowException(ex.Message, ex1);  
                    throw inner;  
                }  
            }
            Console.Read();
        }  
    }  
}
 //值得注意的是:在實例化的時候調用的是PayOverflowException(string message, Exception inner)構造函數,
 //若是本程序若是有其餘程序在調用的時候, 能夠經過.InnerExcetpion的Message屬性進行查看內部異常。
相關文章
相關標籤/搜索