All exception types in C # inherit from System.Exception, that is, System.Exception is the base class for all exception classes. To sum up, its derived classes are divided into two types:
1. SystemException class: All CLR-supplied exception types are derived from SystemException.
2. ApplicationException class: Thrown by a user program to derive a custom exception type that is not typically instantiated directly.
creating custom exception classes should strictly follow several principles
1. Declare serializable (for serialization purposes, of course, if you don't need serialization.) Then it can be declared as serializable.
2. Add a default constructor
3. Add a constructor that contains a message
4. Add a constructor that contains a message and an intrinsic exception type parameter
5. Add a constructor for a serialized information-related parameter.
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.IO;
Using System.Runtime.Serialization.Formatters.Binary;
Namespace ConsoleApplication3
{
[Serializable]//declared serializable because to write to a file
The public class payoverflowexception:applicationexception//is thrown by a user program to derive a custom exception type
{
<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>
Defines a Givebunus virtual method for overloading different derived classes
</summary>
<param name= "Amount" > Bonus limit </param>
public virtual void Givebunus (int amount)
{
Record the value before incrementing with a temporary variable
var pay = Currpay;
This. Currpay + = amount;
if (Currpay > 10000)
{
An exception occurs, and the value of the Currpay is restored.
and throws an exception, and the external program catches the secondary exception
This. Currpay = pay;
var ex = new Payoverflowexception ("The employee's Max pay should is no more than 10000");
Throw ex;
}
}
}
Class Program
{
static void Main (string[] args)
{
Console.WriteLine ("* * * Create an Employee object and use Try/catch to catch exception * *);"
var emp = new Employee (10001, "yilly", 8000);
Try
{
Emp. Givebunus (3000);
}
catch (Payoverflowexception ex)
{
Console.WriteLine ("Exception information: {0}\n occurs in {2} method of {1} class", ex.) Message,
Ex. Targetsite.declaringtype, ex. Targetsite.name);
Try
{
var file = new FileStream (@ "C:\customerexception.txt", FileMode.Create);
Exception information to write code in file omitted ...
Write in serialized format
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's worth noting that: The Payoverflowexception (String message, Exception inner) constructor is invoked at the time of instantiation.
If this program if there are other programs in the call, you can pass. Innerexcetpion the message property to view the inner exception.