佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > C# 自定義異??偨Y(jié)及嚴(yán)格遵循幾個(gè)原則

C# 自定義異??偨Y(jié)及嚴(yán)格遵循幾個(gè)原則

熱門標(biāo)簽:電子圍欄 阿里云 科大訊飛語(yǔ)音識(shí)別系統(tǒng) Linux服務(wù)器 銀行業(yè)務(wù) 服務(wù)器配置 Mysql連接數(shù)設(shè)置 團(tuán)購(gòu)網(wǎng)站
在C#中所有的異常類型都繼承自System.Exception,也就是說(shuō),System.Exception是所有異常類的基類. 總起來(lái)說(shuō),其派生類分為兩種:
1. SystemException類: 所有的CLR提供的異常類型都是由SystemException派生。
2. ApplicationException類: 由用戶程序引發(fā),用于派生自定義的異常類型,一般不直接進(jìn)行實(shí)例化。

創(chuàng)建自定義異常類應(yīng)嚴(yán)格遵循幾個(gè)原則
1. 聲明可序列化(用于進(jìn)行系列化,當(dāng)然如果你不需要序列化。那么可以不聲明為可序列化的)
2. 添加一個(gè)默認(rèn)的構(gòu)造函數(shù)
3. 添加包含message的構(gòu)造函數(shù)
4. 添加一個(gè)包含message,及內(nèi)部異常類型參數(shù)的構(gòu)造函數(shù)
5. 添加一個(gè)序列化信息相關(guān)參數(shù)的構(gòu)造函數(shù).
復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApplication3
{
[Serializable] //聲明為可序列化的 因?yàn)橐獙懭胛募?
public class PayOverflowException : ApplicationException//由用戶程序引發(fā),用于派生自定義的異常類型
{
/// summary>
/// 默認(rèn)構(gòu)造函數(shù)
/// /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>
/// 定義一個(gè)GiveBunus的虛方法以供不同的派生類進(jìn)行重載
/// /summary>
/// param name="amount">獎(jiǎng)金額度/param>
public virtual void GiveBunus(int amount)
{
//用一個(gè)臨時(shí)變量記錄遞增之前的值
var pay = CurrPay;
this.CurrPay += amount;
if (CurrPay > 10000)
{
//發(fā)生異常,將CurrPay的值進(jìn)行恢復(fù),
//并拋出異常,外部程序捕獲次異常
this.CurrPay = pay;
var ex = new PayOverflowException("The employee's max pay should be no more than 10000.");
throw ex;
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("**** 創(chuàng)建Employee對(duì)象,并用try/catch捕獲異常 *****");
var emp = new Employee(10001, "Yilly", 8000);
try
{
emp.GiveBunus(3000);
}
catch (PayOverflowException ex)
{
Console.WriteLine("異常信息:{0}\n發(fā)生于{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();
//以字節(jié)方式寫入
//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;
}
}
}
}
}

值得注意的是:在實(shí)例化的時(shí)候調(diào)用的是PayOverflowException(string message, Exception inner)構(gòu)造函數(shù),
如果本程序如果有其他程序在調(diào)用的時(shí)候, 可以通過(guò).InnerExcetpion的Message屬性進(jìn)行查看內(nèi)部異常。
您可能感興趣的文章:
  • 如何用C#創(chuàng)建用戶自定義異常淺析

標(biāo)簽:衢州 萍鄉(xiāng) 棗莊 江蘇 大理 廣元 衡水 蚌埠

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《C# 自定義異??偨Y(jié)及嚴(yán)格遵循幾個(gè)原則》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    建德市| 静海县| 安福县| 中西区| 禹城市| 白水县| 称多县| 武汉市| 长子县| 调兵山市| 玛沁县| 德昌县| 南汇区| 青海省| 如东县| 呈贡县| 鲜城| 湖南省| 龙海市| 诸城市| 如东县| 南阳市| 三穗县| 阳山县| 乐昌市| 蓝山县| 镇雄县| 丹棱县| 雷山县| 十堰市| 江西省| 扬中市| 高清| 弋阳县| 兰坪| 仁化县| 忻州市| 建瓯市| 三亚市| 九龙县| 同心县|