-
C + + has built-in exception handling syntax element try... Catch
- try statement handles normal logic code
- catch statement processing exception logic code
- Exceptions in a try statement are handled by the corresponding catch statement
try { double r = divide(1, 0); } catch(...) { cout << "Divided by zero ..." << endl; }
- C + + throws an exception through the throw statement
double divide(double a, double b) { const double delta = 0.0000000000001; double ret = 0; if( !((-delta < b) && (b < delta)) ) { ret = a / b; } else { throw 0; // Exception of division 0 generated } return ret; }
-
Analysis of C + + exception handling
-
throw exception must be handled by catch
- The current function can handle exceptions, and the program continues to run downward
- If the current function cannot handle the exception, the function stops executing and returns
-
Unhandled exceptions propagate up the function call stack until they are handled, otherwise the program stops running
Programming experiment: a preliminary study of C + + exception handling
#include <iostream> #include <string> using namespace std; double divide(double a, double b) { const double delta = 0.00000000000001; double ret = 0; if( !((-delta < b) && (b < delta)) ) { ret = a / b; } else { throw 0; } return ret; } int main() { try { double r = divide(1, 0); cout << "r = " << r << endl; } catch(...) { cout << "Divided by zero ..." << endl; } return 0; }Output: Divided by zero ...
-
The same try statement can keep up with multiple catch statements
- The catch statement can define the exception types to be handled
- Different types of exceptions are handled by different catch statements
- Any type of exception can be thrown in a try statement
- catch(...) is used to handle all types of exceptions
- Any exception can only be caught once
- Matching rules for exception handling
Programming experiment: exception type matching
#include <iostream> #include <string> using namespace std; void Demo1() { try { throw 'c'; } catch(short c) { cout << "catch(short c)" << endl; } catch(int c) { cout << "catch(int c)" << endl; } catch(char c) { cout << "catch(char c)" << endl; } catch(...) { cout << "catch(...)" << endl; } } void Demo2() { throw "D.T.Software"; } int main() { Demo1(); try { Demo2(); } catch(char* s) { cout << "catch(char* s)" << endl; } catch(const char* cs) { cout << "catch(const char* cs)" << endl; } catch(string ss) { cout << "catch(string ss)" << endl; } catch(...) { cout << "..." << endl; } return 0; }
Output: catch(char c) catch(const char* cs)Summary
- The concept of directly supporting exception handling in C + +
- Try... Catch... Is a special statement for exception handling in C + +
- try statement handles normal logic code, catch statement handles exception
- The same try statement can keep up with multiple catch statements
- Exception handling must be strictly matched without any type conversion
For the above content, please refer to the series of courses of Ditai Software Institute, and protect the original