1. Build a strong type exception to indicate that the unique details of the current problem will be better.
Suppose you want to build a custom exception named carisdeadexception to indicate the error of the car destined for damage to the acceleration.
1. Create a new class derived from system. Exception/system. applicationexception (according to the conventions, all classes should end with the suffix "exception", which is the best practice of. net ).
1 namespace customexception 2 {3 Public class carisdeadexception: applicationexception 4 {5 private string messagedetails = string. empty; 6 Public datetime errortimestamp {Get; set;} 7 Public String causeoferror {Get; set;} 8 9 Public carisdeadexception () {} 10 11 Public carisdeadexception (string message, string cause, datetime time) 12 {13 messagedetails = message; 14 causeoferror = cause; 15 errortimestamp = time; 16} 17 18 // rewrite exception. message attribute 19 public override string message20 {21 get22 {23 return string. format ("Car error message: {0}", messagedetails); 24} 25} 26} 27}
2. Exception
The exception thrown from accelerate () is very direct. You only need to assign, configure, and trigger a carisexception type, instead of the system. Exception exception type.
1 public void Accelerate(int delta)2 {3 ...... 4 5 CarIsDeadException ex = new CarIsDeadException(string.Format("{0} has overheated!", PetName), "You have a lead foot", DateTime.Now);6 ex.HelpLink = "http://www.CarsRus.com"; 7 throw ex;8 ......9 }
3. Capture exceptions
1 namespace CustomException 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 Car myCar = new Car("Rusty", 90); 8 9 try10 {11 myCar.Accelerate(50);12 }13 catch (CarIsDeadException e)14 {15 Console.WriteLine(e.Message);16 Console.WriteLine(e.ErrorTimeStamp);17 Console.WriteLine(e.CauseOfError);18 }19 }20 }21 }
Generally, you only need to create a custom exception when the wrong class is closely related to the error (for example, a custom file class causes many file-related errors, A car class causes many car-related errors, and a data access object causes errors about a specific database table ). In this way, the caller can handle many exceptions one by one.
Ii. Improvement
To configure custom error information, the current carisdeadexception class overrides the system. Exception. Message attribute and provides two custom attributes to describe other data.
In fact, instead of overwriting the message virtual attribute, you only need to pass the passed information to the constructor of the parent object as follows:
1 namespace customexception 2 {3 Public class carisdeadexception: applicationexception 4 {5 Public datetime errortimestamp {Get; set;} 6 Public String causeoferror {Get; set;} 7 8 Public carisdeadexception () {} 9 10 // pass the information to the parent class object constructor 11 Public carisdeadexception (string message, string cause, datetime time) 12: Base (Message) 13 {14 causeoferror = cause; 15 errortimestamp = time; 16} 17} 18}
In many cases, the role of a custom exception class is not to provide additional functions beyond the inheritance base class,Instead, it provides a strong naming type that clearly identifies the error type,Therefore, the customer will provide different processing procedures for different types of exceptions.
3. Construct a rigorous and standardized custom exception class
1. inherit from the exception/applicationexception class;
2. There are [system. serializable] special tags;
3. Define a default constructor;
4. Define a constructor that sets the inherited message attributes;
5. Define a constructor to handle internal exceptions;
6. Define a constructor for processing type serialization.
Example:
1 [Serializable] 2 public class CarIsDeadException : ApplicationException 3 { 4 public CarIsDeadException() { } 5 public CarIsDeadException(string message) 6 :base(message) 7 { 8 9 }10 public CarIsDeadException(string message, System.Exception inner)11 :base(message ,inner)12 {13 14 }15 protected CarIsDeadException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)16 :base(info, context)17 {18 19 }20 ......21 }
Visual Studio provides a code snippet template called "exception", which can automatically generate a new exception class that complies with. Net best practices. Enter "exc" and press the tab twice to activate the code.