国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

C# 自定義異常總結及嚴格遵循幾個原則

2024-07-10 13:23:13
字體:
來源:轉載
供稿:網友
在C#中所有的異常類型都繼承自System.Exception,也就是說,System.Exception是所有異常類的基類. 總起來說,其派生類分為兩種:
1. SystemException類: 所有的CLR提供的異常類型都是由SystemException派生。
2. ApplicationException類: 由用戶程序引發,用于派生自定義的異常類型,一般不直接進行實例化。

創建自定義異常類應嚴格遵循幾個原則
1. 聲明可序列化(用于進行系列化,當然如果你不需要序列化。那么可以不聲明為可序列化的)
2. 添加一個默認的構造函數
3. 添加包含message的構造函數
4. 添加一個包含message,及內部異常類型參數的構造函數
5. 添加一個序列化信息相關參數的構造函數.

復制代碼 代碼如下:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApplication3
{
[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>獎金額度</param>
public virtual void GiveBunus(int amount)
{
//用一個臨時變量記錄遞增之前的值
var pay = CurrPay;
this.CurrPay += amount;
if (CurrPay > 10000)
{
//發生異常,將CurrPay的值進行恢復,
//并拋出異常,外部程序捕獲次異常
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("**** 創建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;
}
}
}
}
}


值得注意的是:在實例化的時候調用的是PayOverflowException(string message, Exception inner)構造函數,
如果本程序如果有其他程序在調用的時候, 可以通過.InnerExcetpion的Message屬性進行查看內部異常。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 芒康县| 札达县| 洛扎县| 屏东县| 天祝| 黄浦区| 沧州市| 弋阳县| 白玉县| 宿松县| 盱眙县| 阳城县| 绍兴县| 苍南县| 镶黄旗| 山东| 渝北区| 谷城县| 哈密市| 蒲城县| 巢湖市| 陕西省| 耿马| 北宁市| 蒙自县| 洛扎县| 大足县| 凭祥市| 景德镇市| 青州市| 临沂市| 洛阳市| 凤台县| 工布江达县| 青神县| 石楼县| 达拉特旗| 金沙县| 太白县| 建始县| 玉环县|