C# 自定义异常总结及严格遵循几个原则

 更新时间:2021年9月22日 10:17  点击:2253
在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 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("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属性进行查看内部异常。
[!--infotagslink--]

相关文章

  • C# 自定义异常总结及严格遵循几个原则

    在C#中所有的异常类型都继承自System.Exception,也就是说,System.Exception是所有异常类的基类. 总起来说,其派生类分为两种,需要了解的朋友可以参考下...2021-09-22
  • Java构造方法 super 及自定义异常throw合集详解用法

    异常是程序中的一些错误,但不是所有错误都是异常,且错误有时候是可以避免的,super可以理解为是指向自己超(父)类对象的一个指针,而这个超类指的是离自己最近的一个父类,构造器也叫构造方法、构造函数,是一种特殊类型的方法,负责类中成员变量(域)的初始化...2021-10-23
  • Python用户自定义异常的实现

    这篇文章主要介绍了Python用户自定义异常的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-25
  • SpringBoot Security的自定义异常处理

    这篇文章主要介绍了SpringBoot Security的自定义异常处理方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-12-20