C# 自定義異??偨Y(jié)及嚴(yán)格遵循幾個(gè)原則
更新時(shí)間:2012年12月21日 11:49:53 作者:
在C#中所有的異常類型都繼承自System.Exception,也就是說,System.Exception是所有異常類的基類. 總起來說,其派生類分為兩種,需要了解的朋友可以參考下
在C#中所有的異常類型都繼承自System.Exception,也就是說,System.Exception是所有異常類的基類. 總起來說,其派生類分為兩種:
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ù).
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í)候, 可以通過.InnerExcetpion的Message屬性進(jìn)行查看內(nèi)部異常。
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í)候, 可以通過.InnerExcetpion的Message屬性進(jìn)行查看內(nèi)部異常。
您可能感興趣的文章:
相關(guān)文章
淺談Asp.Net母版頁(yè)和內(nèi)容頁(yè)運(yùn)行機(jī)制
這篇文章主要介紹了淺談Asp.Net母版頁(yè)和內(nèi)容頁(yè)運(yùn)行機(jī)制,詳細(xì)的介紹了母版頁(yè)和內(nèi)容頁(yè)的運(yùn)行過程步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11asp.net button 綁定多個(gè)參數(shù)
asp.net button 綁定多個(gè)參數(shù)的代碼2008-11-11Asp.Net 程序錯(cuò)誤Runtime Error原因與解決
提示這個(gè),不管怎么改配置文件的設(shè)置都不行,下面是修正方法,大家可以試試。2010-03-03關(guān)于.NET Framework中的設(shè)計(jì)模式--應(yīng)用策略模式為L(zhǎng)ist排序
本篇文章,小編將為大家介紹關(guān)于.NET Framework中的設(shè)計(jì)模式--應(yīng)用策略模式為L(zhǎng)ist排序,有需要的朋友可以參考一下2013-04-04.NET?API?接口數(shù)據(jù)傳輸加密最佳實(shí)踐記錄
這篇文章主要介紹了.NET?API?接口數(shù)據(jù)傳輸加密最佳實(shí)踐記錄,我們?cè)谧?Api?接口時(shí),相信一定會(huì)有接觸到要給傳輸?shù)恼?qǐng)求?body?的內(nèi)容進(jìn)行加密傳輸。其目的就是為了防止一些敏感的內(nèi)容直接被?UI?層查看或篡改,需要的朋友可以參考下2022-10-10ASP.NET?MVC5?網(wǎng)站開發(fā)框架模型、數(shù)據(jù)存儲(chǔ)、業(yè)務(wù)邏輯(三)
上次搭建好了項(xiàng)目框架,但還是覺得不太對(duì)勁,后來才想起來沒有對(duì)開發(fā)目標(biāo)進(jìn)行定位,這個(gè)小demo雖然不用做需求分析,但是要實(shí)現(xiàn)什么效果還得明確。后來想了一下就做個(gè)最簡(jiǎn)單的網(wǎng)站,目標(biāo)定為小公司進(jìn)行展示用的網(wǎng)站。功能有顯示用的文章功能,咨詢留言,評(píng)論等2015-09-09在Asp.net用C#建立動(dòng)態(tài)Excel
在Asp.net用C#建立動(dòng)態(tài)Excel...2006-09-09