Abnormalities in C
Ovenaly-handled use
First, don't give an exception, you will be dead (of course, it is troubled)!
In C programming, I adhere to this idea, that is: I can use an abnormally handling. Because the confusion caused is too much - there are more. If you can capture the error and process it with other methods, you don't have an exception! Oh, perhaps a little extreme, but I think this is a good way to avoid unnecessary mistakes. What allocated memory failed, open the file failed, and we only need to use the function such as Assert, Abort to solve the problem. That is, if there is enough information to process an error, then this error is not an abnormality.
Of course, the existence of abnormal processing also has its own meaning and role. Not you say no need, some places must not be used!
For example, in the current context environment, the type of error cannot be captured or determined, we have to use an exception to go to a larger context environment. Also, the use of abnormal processing, allows the error handler to divide the "usual" code, making the code more concise and more flexible. In addition, the procedure must be essential, and abnormal handling often plays an important role.
OK, explain it below.
2. Throw an abnormality
Off - key (Zhou Xingchi's tone): throw
Example: Throw ExceptionClass ("OH, Shit! It's A Exception! L");
In the example sentences, ExceptionClass is a class that is used as a parameter in a string to illustrate an exception. That is, when throw, the C compiler first constructs an ExceptionClass object, let it be the return value of the throw, throw - out. At the same time, the program returns, and the destructor is called. Look at the following program:
#include
Class ExceptionClass {
Char * name;
PUBLIC:
ExceptionClass (char * name = "default name") {
COUT << "construct" << name << endl;
This-> name = name;
}
~ EXceptionclass () {
Cout << "destruct" << name << endl;
}
Void mythrow () {
Throw ExceptionClass ("O, My GOD");
}
}
Void main () {
ExceptionClass E ("Haha");
Try {
e.mythrow ();
} Catch (...) {
}
}
Everyone knows the results, after throw, call the current descent, the whole end of this class's historical mission.唉 ~~
3. Expertise description
If we call someone else's function, there is an abnormally thrown, I use it to see the source code to see what exception is there? Yes, but too - irritability. A preferred solution is to write a function with an abnormally thrown function, which uses an exceptional specimen that makes us see which abnormality appears.
Abnormal compact description is generally the following format: void ExceptionFunction (argument ...) throw (ExceptionClass1, ExceptionClass2, ....)
By the way, all the abnormal classes have been explained in the parentheses of throw () at the end of the function, so that the function caller is a clear!
Note the following form:
Void ExceptionFunction (Argument ...) throw ()
It indicates that there is no exception thrown.
Normal Void ExceptionFunction (argument ...) said: may throw any anomalies, of course, may have no abnormalities, the meaning is the most wide.
4. Abnormally throwing abnormality in structural and sectors
55555, there should be a place to pay attention.
First look at a program, if I throw an exception in the place where the constructor is throwing, is this sectarian description? Can you not be released if you don't call? ?
program:
#include
#include
Class ExceptionClass1 {
Char * S;
PUBLIC:
ExceptionClass1 () {
COUT << "exceptionclass1 ()" << endl;
s = new char [4];
COUT << "throw a exception" << endl;
Throw 18;
}
~ EXceptionclass1 () {
Cout << "~ exceptionclass1 ()" << Endl;
Delete [] S;
}
}
Void main () {
Try {
EXCEPTIONCLASS1 E;
} catch (...)
{}
}
The result is:
EXCEPTIONCLASS1 ()
Throw a Exception
No, no, here! However, but between these two sentences, we have assigned memory to s, where is it? Is the memory released? No, no, because it is released in the destructor, wow! The problem is big. How to do? How to do?
In order to avoid this, avoid the object through its own constructor involves an abnormality. That is, there is neither an abnormally thrown in the constructor, and an abnormality should not be thrown in everything that constructor calls. Otherwise, only the easter.
So what about the situation in the destructor? We already know that after the abnormality throws, we must call itself the destructive function. If there is any abnormality in this parsing function, the existing exception has not been captured, which will cause an exception to capture.
Others, that is, we don't have an exception thrown in the constructor and the destructor.
5. Abnormal capture
The programs on the upper side don't know if you understand, the abnormal capture has appeared above.
That's right, it is try {...} catch (...) {...} This structure!
In the latch lack of TRY, it is possible to involve various statements such as an abnormality. If there is an abnormality, it will be captured by the abnormality processor intercept, and go to catch processing. First, compare the class in the back of the abnormal class and the back of the Catch, if consistent, turn to the rear curly brackets.
For example, throw an exception is written: void f () {throw exceptionclass ("ya, j");}
Suppose the class ExceptionClass has a member function function () to process or corresponding messages when there is an abnormality (just do an example, don't pick my thorn).
So, I can catch: try {f ()} catches (ExceptionClass E) {E.Function ()};
Of course, like the above programs, I can use three points after catching all anomalies. Such like try {f ()} catch (...) {}. This truncates all abnormalities. Helps all unpredictable abnormal shields (I think that J).
After an exception capture, I can throw it again, use a throw statement without any parameters, for example: try (f ()) catch (...) {throw}
6. Standard exception
As many people think, C must have its own standard anomaly.
A total base class:
Exception is a base class for all C abnormalities.
Two exception classes are derived below:
The logic error of the logic_erro report program can be detected before the program is executed.
Runtime_erro as the name suggests, the reporter runs when running, only when running can be detected.
The above two have their own derived classes:
Abnormal class derived from logic_erro
Domain_error report violates the pre-condition
INVALID_ARGUMENT indicates an invalid parameter of the function
Length_ERROR pointed out an attempt to generate an object that produces more than NPOS lengths (NPOS is the maximum performance value of Size_t)
OUT_OF_RANGE report parameter crossing
BAD_CAST has an invalid Dynamic_cast expression in the runtime type identification
BAD_TYPEID report has an empty pointer P in expression typeid (* p)
Unusual derived from Runtime_ERROR
Range_Error report violates the back conditions
Overflow_ERROR reports an arithmetic overflow
BAD_ALLOC report a storage assignment error
Page, this is a summary report for my two days. Call, tired.