Self-built toolset development document ------ Abnormal processing (1.0.0.1)
version number
founder
Create time
Note
1.0.0.1 Musi Road 2003-12-20 Draft
Keywords: C #, tool set, exception, error
text:
The .NET Framework provides a complete and powerful exception handling mechanism, where the main purpose of this exception handling class is to handle exceptions, and log logs, which is convenient for testing and maintenance. And you can put it in other systems and don't need to make a procedure change, just modify some of the configuration information.
Design goals
1. Use custom abnormal messages to give user prompts and exception information.
2. Simplify the complexity of the abnormal class call as much as possible.
3. Exactly locate an abnormal position.
Abnormal processing mechanism of .NET Framework (this section is taken from .NET Help documentation)
The exception in C # provides a structured, unified and type of security for processing system-level and application-level error states. All exceptions must be represented by an instance of class types derived from System.Exception. When an exception occurs, the system will search for the most recent Catch clause of the exception, which is determined by the abnormal runtime type. First, search the current method to find a closed TRY sentence on a lexical, and consider the CATCH clause associated with the try statement in order. If the above operation fails, search for the method of calling the current method to find a closed TRY sentence on a lexist, which encloses the location where the current method is called. This search will continue until you find a Catch clause that can handle the current exception (by specifying the running time type of the abnormality that is the same class or base class). The Catch clause that does not specify an exception class can handle any exceptions.
After finding the matching catch clause, the system will transfer the control to the first statement of the CATCH clause. Before the execution of the CatCH clause, the system will first perform any of the finally clauses associated with the TRY degree of the nested to capture the Try statement of the exception in order.
If you do not find a matching catch clause, one of the following two cases occur:
● If the search for the matching catch clause reaches the static constructor or the static field initial value setting item, the SYSTEM.TYPEINITIALIZATIONException is triggered in the place that triggered the call to the static constructor. The internal exception of System.TypeInitializationException includes an exception initiated.
● If the search of the matching catch clause reaches the code initiated the thread, the execution of the thread will be terminated. This type of termination is defined by implementation.
It is particularly worth noting that an exception that occurs during the execution of the destructor. If an exception occurs during the execution of the destructor and the exception is not captured, the execution of the destructor will be terminated and the destructor of the base class is called (if any). This exception will be abandoned if there is no base class (such as the case in system.object), or if there is no basic class destructor function.
Design ideas:
There are two exceptions in the system design and operation, one is an exception thrown by the system, which is dependent on .NET Framework, can be captured through the TRY block, such as the database opens; the other is customizable Exceptions, such as the exception caused by the input data format errors, perhaps it should only be said to be a mistake.
Treatment:
1. Use the TRY block to capture the abnormality that the system may cause, turn into its own abnormality, throw, do not do other processing;
2. If you need to trigger an exception, instantiate a self-abnormality and throw, don't do other;
3. Under normal circumstances, all intermediate processing processes do not treat an exception thrown on the lower layer, nor to capture; 4. The uppermost layer, that is, a layer of direct and interface interaction, capture all anomaly, all subtracks thrown Exception, only need to remove the message displayed from the exception to the user, and the exception caused by this hierarchy system is processed according to the first.
The following is given below:
Abnormal information configuration file
ExceptionConfig.xml is stored in XML files, the file format is as follows:
XML Version = "1.0" encoding = "UTF-8"?>
Exceptionconfig>
Document description:
Root contact name: ExceptionConfig, please pay attention to case;
Each Exception contact corresponds to an exception information, the meaning of each field is as follows:
* ID: Abnormal number, used for an abnormality within the file, will also display this error number when displaying an error message to the user, from 1st, 999 is a custom default unusual information, and cannot modify this node.
* Name: The name of the abnormality is used in the coding quote, so in this file, the name of each abnormality cannot be repeated, otherwise the foremost exception is taken, and the backpoint will be overwritten. Naming Rules Suggestions: Start with Error, split each word with underscore, can see the abnormal alteration from the name. * UserMsg: When the user message, this information will be displayed directly to the user.
* Logmsg: Log messages, when an exception occurs, the information of the log file will be recorded, and the description is clearly described.
* Level: Abnormal level, the severity of the logo is abnormal, with the value from 1 to 5 (including 1, 5), the higher the severity, the greater the value.
Abnormal description
Field:
PRIVATE STRING EXCETIONNAME = NULL;
Private int exceptionno = 0;
Private constant.exceptionLevel ExceptionLevel;
PRIVATE STRING UserMsg = NULL;
PRIVATE STRING LOGMSG = NULL;
* ExceptionName: The name of the exception, corresponds to the NAME in the XML file.
* ExceptionNO: Exception number, used to display the end user, and the ID in the XML file corresponds.
* ExceptionLevel: Abnormal level, enumeration type, defined in constants, with values from 1 to 5, corresponding to the level field in the XML file.
* UserMSG: User message, and the usermsg in the XML file corresponds.
* Logmsg: log messages, and corresponding LOGMSG in the XML file.
Attributes:
Public String Usermsg {GET, SET}
Public string logmsg {get, set}
Public constructor:
Public utilexception () {}
Public utilexception (string _userMessage, string _logmessage, constant.exceptionLevel _exceptionLevel) {}
Public utilexception (string _exceptionname) {}
Public utileXception (System.Exception E, String _usermsg) {}
* _EXCEPTIONNAME: The name of the abnormality, and the NAME in the XML file, the constructor will obtain an error description information from the XML file through this field to initialize the various fields of the anomaly class.
* You can provide exceptions in the program, log messages, grades to initialize exception, but do not promote this, it will bring difficulties to maintenance.
* Try to use System.exception to instantiate your own exception class, which automatically records the stack information to the log.
* You can initialize an exception with an exception, and the constructor gets exception information from the XML file.
Method Description:
Private void writelog ()
Private string getLogmsg ()
Private string getExceptionClass () private string getinnerMessage ()
All methods in the program are private, in the constructor, WriteLog () will be called, where method getExceptionClass () is used to get the stack information in Innerexception, GetInnerMessage () is used to get the error message in Innerexception, Empty the anomaly class that is initialized.
Existing problems:
* Initialization of its initialization, cannot obtain the stack information when the abnormality is raised, only the stack information when you initialize your anomaly class.
* Setting of abnormal levels, the original idea is to do corresponding processing according to the different levels of the error, even depending on the level, the switch to the log function is configured.
Conclusion:
I just achieved a very simple feature, and the introduction here is also very rough. There is no detailed implementation details of the internal, there is a friend who is interested in the source code 191471650@qq.com, first introduce this class. This class will be used in order to be used later.
Such an implementation will definitely make everyone disappeared, this is also the result of closed the door. I hope that all friends will give me some improvement opinions, thank you very much, I will continue to improve and continue to work hard.