All exception types in C# are inherited from System.Exception, that is to say, System.Exception is the base class of all exception classes.
1. SystemException class: All CLR-provided exception types are derived from SystemException.
2. ApplicationException class: User program triggered, used to derive custom exception types, usually not directly instantiated.
3. When customizing exception classes, they are usually derived from Application Exception, and only when developing an extended class library of the. NET Framework, they are considered to be derived from System Exception.
Several principles should be strictly followed in creating custom exception classes
- Statements can be serialized (for serialization, if you don't need serialization, of course). Then it can not be declared serializable;
- If your exception needs to be written to a file, such as a log, you need to declare the exception class Serializable.
- Add a default constructor to implement a parameterless constructor, because parameterless exceptions may be thrown
- Add a constructor containing message
- Add a constructor containing message and internal exception type parameters
- Add a constructor for serialized information-related parameters.
- Add your own error-recognition data members and handlers
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApplication3
{
[Serializable] //Declare serializable because you want to write to a file
public class PayOverflowException : ApplicationException//Raised by a user program to derive custom exception types
{
/// <summary>
/// Default constructor
/// </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>
/// Define a GiveBunus A virtual method for overloading different derived classes
/// </summary>
/// <param name="amount">Bonus limit</param>
public virtual void GiveBunus(int amount)
{
//Record values before increment with a temporary variable
var pay = CurrPay;
this.CurrPay += amount;
if (CurrPay > 10000)
{
//When an exception occurs, the value of CurrPay is restored.
//And throw an exception, the external program catches the secondary exception
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("**** Establish Employee Objects, and try/catch Capture exception *****");
var emp = new Employee(10001, "Yilly", 8000);
try
{
emp.GiveBunus(3000);
}
catch (PayOverflowException ex)
{
Console.WriteLine("Abnormal information:{0}\n Happen to{1}Class{2}Method", ex.Message,
ex.TargetSite.DeclaringType, ex.TargetSite.Name);
try
{
var file = new FileStream(@"c:\customerexception.txt", FileMode.Create);
//*** Code omission of exception information written to file ***
//Write in a serialized manner
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(file, ex);
file.Close();
//Write in bytes
//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;
}
}
}
}
}
It is worth noting that the PayOverflow Exception (string message, Exception inner) constructor is called when instantiating.
If there are other programs in this program when calling, you can view internal exceptions through. InnerExcetpion's Message property.
http://blog.csdn.net/wangweitingaabbcc/article/details/6851717