Notes: 2.6
Each programmer knows that in the process of running, some cases are unpredictable, regardless of how much the program is designed, it will errors in a particular environment, but such an error will often work together. At, for example, NEW may be insufficient. Obviously this error is obvious. It cannot be fully said to be a problem of programming, but if your program can react to these situations, such program practicability will be stronger. The abnormal mechanism is for this end.
Note that the word "mechanism" is used here, and it is also the rules of this section, just as the beginning of this section: Everyone has its own way of handling. For example, use the IF statement. There is no problem, but our life is in an unfortunate era, the scale of the program is far from the extent that the personal ability can accept. We need to cooperate, so unified coding style has become a necessary thing, which is why programmers often tell beginners to program a reason for one of the most personalized things,
There is also a benefit using an abnormal mechanism. He can greatly reduce the length and scale of the code. Connect a unified process for the same abnormality. This is very empty, let's give a small example. SOLVESCTION We write a function of the same unit for the INT, CHAR's pointer, if not an abnormal mechanism, write:
Bool new_space (int * & pev, char * & ch, int size) {
PEV = New Int [size];
CH = new char [size];
IF (! PEV)
Return False;
IF (! CH)
RETUEN FALSE;
Return True;
}
This thing is probably no one likes, because if we call him like this, New_Space (P, C, 6), regardless of the spatial distribution success or not. Callors don't know (Q: You don't define a BOOL quantity? A: Will. I am bother. I use another), grace, you have to make our things in humanization. Modify as follows:
Bool new_space (int * & pev, char * & ch, int size) {
PEV = New Int [size];
CH = new char [size];
IF (! pEV) {
CERR << "Err1" << endl; // In the help system, it means that Err1 is insufficient.
Return False;
}
IF (! CH) {
CERR << "ERR1" << Endl;
RETUEN FALSE;
}
Return True;
}
Oh, do you have a cute user? But I found some sorry. CERR << "Err1" << Endl; Retuen False; I wrote twice, this is two pointers, if ten? God. Boss, processing money (answer you must know), well, try an abnormal mechanism, change it.
Bool new_space (int * & pev, char * & ch, int size) {
Try
{
String Word ("Err1");
PEV = New Int [size];
CH = new char [size];
IF (! PEV)
Throw Word;
IF (! CH)
Throw Word;
}
Catch (String Err)
{
CERR << Err << endl; returnaf;
}
Return True;
}
Oh, this is more comfortable. Everyone may see that the function above does not seem to be reduced, yes, but if the process is not only two, there are many, you will find that he saves you a lot of strength.
The above mainly wants to explain the benefits and usage of abnormal mechanisms (limited horizon, may be not good) in this section, that is, the program handles exceptions and orders. For example, the above program, we want it to show OK when the assignment is successful, then cout << "ok" << Endl; what is written? What should I do if the exception thrown in our try does not have the corresponding catch in the function? It is very detailed in these books. Take a closer look, you must have a lot of harvest.