In the development process of C + +, the exception handling mechanism is introduced for the actual needs. Common errors in the program: syntax errors and run errors , syntax errors are generally found at compile time, the compiler will basically report the exact location of the error, so this kind of error is generally better to modify, run errors are generally not easy to debug, for example, The program crashes (usually due to stack overflow), the operation result error (usually the logical structure of the algorithm is problematic), the program abnormal termination and so on. The introduction of exception handling in C + + (handling errors at runtime) can greatly improve the fault tolerance of the program.
C + + handles exceptions by using the try (check), throw (throw), catch (catch) three parts to be processed, put the code that is likely to appear the exception in the try block, throw to throw an exception information, the catch parameter is generally thrown information type, If the thrown type is the same as the catch catch type, the corresponding exception is handled.
The following is an example of exception handling:
#include <iostream>using namespace std; #include <math.h>/* defines a function to calculate the area of a triangle (using the Helen formula s = sqrt (q* (q - a) * (q - b) * (q - c)), where q = (a+b+c)/2) Meet the conditions of the triangle: A+b>c a+c>b b+c>a (A,b,c are greater than 0) */double triangle (double a, &NBSP;DOUBLE&NBSP;B,&NBSP;DOUBLE&NBSP;C) { double q = (a + b + c) / 2; if (a + b <= c | | a + c <= b | | b + c <= a) //a,b,c These three lengths do not form a triangle { throw a; //Throw Exception } return sqrt (q * (q - a) * (q - b) * (q - c)); &NBSP;&NBSP;&Nbsp;//Triangle Area}int main () { double a, b, c; cin >> a >> b >> c; try //check for an exception, the exception appears in two aspects, one is the a,b,c may be negative, or both sides and not greater than the third side { while (a > 0 && b > 0 && c > 0) { Cout << triangle (a, b, c) << endl; cin >> a >> b >> c; } } catch (double) //catching exceptions and handling { cout << a << " " << b << " " << c << "this is not triangle!" << endl; } system ("pause"); return 0;}
In the above example, the three-side length of the triangle may not meet the conditions that make up the triangle, an exception occurs, in the try block is the code of the possible exception, in the triangle function, the length of the three sides of the condition of the judgment, if not satisfied, the exception is thrown, At this point the program will return to the main function to find the corresponding catch block, the above throw A;a data type is a double type, and the type of catch is also a double type, which matches, executes the processing code in the CATCH block, The program then does not return the location that was thrown by the called function, but executes the code immediately after the catch.
Attention:
(1) The code in the try block does not affect the order in which it was executed, and if no exception occurs in the try, the code in the catch block is not executed and the code after the catch is executed directly. If an exception occurs, the exception is thrown, and the process immediately leaves the function, returning the function at the previous level, without executing the code following the throw of this function.
(2) When the process returns to the previous function, the system looks for the catch block that matches it and executes it. Executes the code in the Catch block, followed by the code after the catch block, until the end of the program.
(3) the part being detected must be placed in a try block, otherwise it will not work. Catch blocks cannot be used alone, but try blocks can be used alone, but only for inspection, without error handling. The try and catch blocks must be enclosed in curly braces, even if there is only one statement in the block. There can be only one try block in a program, but there are multiple catch blocks that can handle multiple problems. Catch checks only the type of the parameter and does not check the value of the parameter.
(4) If catch (Doube b) {}; is to compare the double type of the parameter with the type of throw a, and then assign the value of a to B, which is b=a;
If catch (...) {}; The code in the catch block is executed regardless of the type. Usually such a catch will be placed at the end of the program, for some unexpected exceptions to be processed, if placed at the beginning of the catch will not be executed.
(5) Try-catch can and throw in the same function, or not in the same function, if not in a function, their catch to perform the satisfied relationship:
650) this.width=650; "title=" Untitled. png "alt=" Wkiom1dc-8bsu708aaauy4ua38c733.png "src=" http://s4.51cto.com/wyfs02/M02 /80/7f/wkiom1dc-8bsu708aaauy4ua38c733.png "/>
(6) If the throw exception information does not find a matching catch, the system will automatically call a system function terminate function, so that the program stops running.
This article from "Unintentional persistent" blog, reproduced please contact the author!
C + + ' exception ' processing mechanism