1. targetsite attributes (Public methodbase targetsite {Get ;})
The system. Exception. targetsite attribute helps us understand the information about the methods that cause an exception. The value of the output targetsite will display the return value type, method name, and parameters that cause the exception method.
Instead of returning only strings, it returns a strongly typed system. reflection. methodbase object.
1 Console. writeline ("member name: {0}", E. targetsite); // output result: void accelerate (int32) 2 console. writeline ("class defining Member: {0}", E. targetsite. declaringtype); // output result: leleapplication3.car3 console. writeline ("member type: {0}", E. targetsite. membertype); // output result: Method
Note:
1. We use the methodbase. declaringtype attribute value to specify the full name of the class that causes the exception (consoleapplication3.car in this example ).
2. Use the membertype attribute of the methodbase object to determine the Member types (such as attributes and methods) that cause the exception ).
Ii. stacktrace attributes (Public Virtual string stacktrace {Get ;})
The system. Exception. stacktrace attribute helps us identify some column calls that cause exceptions.Note that the value of stacktrace is automatically generated when an exception is created and cannot be assigned a value.
Example:
1 catch(Exception e)2 {3 Console.WriteLine("Stack: {0}", e.StackTrace);4 }
Output:
STACK: At leleapplication3.car. Accelerate (int32 delta) Location E: \ test \ consoleapplication3 \ consoleapplication3 \ Program. CS: Row 66 identifies the first call to this sequence
In leleapplication3.program. Main (string [] ARGs) Location E: \ test \ consoleapplication3 \ consoleapplication3 \ Program. CS: Row 18 identifies the specific location of the error member
This information is useful when debugging or recording the creation of an application. It allows us to naturally find the root cause of the error.
Iii. helplink attributes (Public Virtual string helplink {Get; set ;})
By default, the value of the helplink attribute is an empty string. If you need a meaningful value to fill this attribute,The value must be assigned before the system. Exception type exception is thrown.
Example:
Update the accelerate method in the previous example.
1 Public void accelerate (INT delta) 2 {3 if (casisdead) 4 {5 console. writeline ("{0} is out of order... ", petname); 6} 7 else 8 {9 currentspeed + = delta; 10 if (currentspeed> = maxspeed) 11 {12 casisdead = true; 13 currentspeed = 0; 14 15 // we need to call the helplink attribute. Therefore, we need to create a local variable 16 exception EX = new exception (string. format ("{0} has overheated! ", Petname); 17 ex. helplink = "http://www.CarsRus.com"; 18 throw ex; 19} 20 else21 {22 console. writeline ("=> currentspeed = {0}", currentspeed); 23} 24} 25}
4. data attributes (Public Virtual idictionary data {Get;})
The data attribute in system. exception allows us to use the response information provided by the user to fill in the exception object.
Update the accelerate method:
1 Public void accelerate (INT delta) 2 {3 if (casisdead) 4 {5 console. writeline ("{0} is out of order... ", petname); 6} 7 else 8 {9 currentspeed + = delta; 10 if (currentspeed> = maxspeed) 11 {12 casisdead = true; 13 currentspeed = 0; 14 15 // we need to call the helplink attribute. Therefore, we need to create a local variable 16 exception EX = new exception (string. format ("{0} has overheated! ", Petname); 17 ex. helplink = "http://www.CarsRus.com"; 18 19 // fill in custom data about error 20 ex. data. add ("timestamp", String. format ("the car exploded at {0}", datetime. now); 21 ex. data. add ("cause", "you have a lead foot. "); 22 throw ex; 23} 24 else25 {26 console. writeline ("=> currentspeed = {0}", currentspeed); 27} 28}
Call:
1 static void main (string [] ARGs) 2 {3 car mycar = new car ("Zippy", 20); 4 5 try 6 {7 for (INT I = 0; I <10; I ++) 8 {9 mycar. accelerate (10); 10} 11} 12 catch (exception e) 13 {14 // by default, the data field is empty. You need to check whether it is blank 15 if (E. data! = NULL) 16 {17 foreach (dictionaryentry item in E. data) 18 {19 console. writeline ("-> {0 }:{ 1}", item. key, item. value); 20} 21} 22} 23}
Output:
=> Currentspeed = 30
=> Currentspeed = 40
=> Currentspeed = 50
=> Currentspeed = 60
=> Currentspeed = 70
=> Currentspeed = 80
=> Currentspeed = 90
-> Timestamp: The car exploded at 16:16:54
-> Cause: You have a lead foot.
Note:
The data attribute is very useful because it allows us to package custom information about "errors" without creating a new class type to extend the exception base class.