2. The basic syntax of exception handling
The following is an example of a basic code, indicating the throw, the try , catch basic usage, the catch automatically match type:
. 1 #include <the iostream> 2 #include < String > . 3 . 4 the using namespace STD; . 5 . 6 int TEST_1 ( int NUM) . 7 { . 8 IF (NUM =! 0 ) . 9 { 10 the throw - . 1 ; // Throws an int type exception, if over 30 Acts 1, then the exception will be thrown try to line 36 . 11 } 12 is the else { 13 is the throw new new string ( " throws exception string "); // throw an exception string, if over 30 Acts 0, then the string will throw an exception, the catch 42 will match line 14 } 15 16 return 0 ; . 17 } 18 is . 19 int test_2 ( int NUM ) // function nesting 20 is { 21 is TEST_1 (NUM); 22 is return 0 ; 23 is } 24 25 int main () 26 is { 27 int RET = 0 ; 28 try // the return value of the method will be below the try the catch catch 29 { 30 RET = test_2 ( 1 ); // pass over 1, will catch the test_1 throw -1, 41 will jump directly to the line. 31 is } 32 the catch ( int error) { // captured value passed to error variable 33 is the printf ( " abnormal of D% \ n-! " , Error); 34 is } 35 the catch ( String * error) 36 { 37 [ the printf ( " capture string exception:% S " , Error-> the c_str ()); //If line 30 is passed in the past 0, can be thrown exception for a proper type String 38 is Delete error; 39 } 40 the catch (...) { // if no suitable type herein will enter a wildcard, If this line is not through with you try to pass a floating-point compiler will not come off. 41 is the printf ( " the catch ... \ n- " ); 42 is } 43 is 44 is return 0 ; 45 }
Here are saying about the situation throws intercepted, the same code above:
If test_2 intercepted an exception in test_1, then the call at the main function will not get an exception, unless it throws again in test_2 2 (such as 29 lines).
. 1 #include <the iostream> 2 #include < String > . 3 . 4 the using namespace STD; . 5 . 6 int TEST_1 ( int NUM) . 7 { . 8 IF (NUM =! 0 ) . 9 { 10 the throw - . 1 ; // Throws an int type abnormality, return to the line 20 . 11 } the else { 12 is the throw new new string ( " throws an exception string " ); 13 } 14 15 return 0 ; 16 } . 17 18 is int test_2 ( int NUM) . 19 { 20 is try // If this exception is caught, the first row 38 will try to catch exceptions not test_1 21 is { 22 is test_1 (NUM); 23 is } 24 the catch (...) { 25 the printf ( " test_2 exception is thrown " ); 26 is the throw 0.01 ; 27 } 28 29 / * the throw * / 30 //Here if we throw, 38 will be received again test_1 line abnormality 31 is 32 return 0 ; 33 is } 34 is 35 int main () 36 { 37 [ int RET = 0 ; 38 is the try 39 { 40 RET test_2 = ( . 1 ); // pass 1 in the past, will be thrown test_1 -1 in the throw 41 is } 42 is the catch ( int error) { 43 is the printf ( " abnormal of D% \ n-! " , error); 44 is } 45 the catch ( String * error) 46 is { 47 the printf ( " capture string exception:% S " , Error-> the c_str ()); 48 Delete error; 49 } 50 the catch (...) { 51 is the printf ( " the catch ... \ n- " ); 52 is } 53 is 54 is return 0 ; 55 }
3. Exception handling interface declaration
4. abnormal type of life cycle
4.1 the throw basic types:
4.2 the throw string type:
4.3 the throw an exception class type:
5. Exceptions and Inheritance
6. The basic idea of exception handling
7. The standard library exception class