#include <iostream> #include <string>using namespace std;double divide (double A, double b) { const double Delta = 0.000000000000001; DOUBLE ret = 0; if (! ( (-delta < b) && (b < delta)) {ret = A/b ; } else { throw 0;//throws exception } return ret;} int main (int argc, char *argv[]) { try { Double r = Divide (1, 0);//divide Divisor is 0 throws an exception, if no try Catch program terminates abnormally C14/>cout << "R =" << r << Endl; } catch (...) //... Represents a catch of any type of exception here divide exception when int type { cout << "divided by zero ..." << Endl; } return 0;}
The type of exception caught in the #include <iostream> #include <string>using namespace std;//try must match the type of exception in Cath exactly! No type conversions//Exceptions for class types apply the assignment compatibility principle, and the subclass's exception matches the parent class's catch statement void Demo1 () {try {throw ' C '; } catch (char c) {cout << "catch (char c)" << Endl; } catch (short c) {cout << ' catch (short c) ' << Endl; } catch (double c) {cout << "catch (double c)" << Endl; } catch (...) //... The statement must be placed at the end, catching any type of exception that is not caught {cout << catch (...) << Endl; }}void Demo2 () {throw string ("D.t.software"),//const char *cs type}int main (int argc, char *argv[]) {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; } return 0;}
The catch statement throws an exception for the purpose of redefining the exception information:
#include <iostream> #include <string>using namespace std;void Demo () {try {try { Throw ' C '; } catch (int i) {cout << "inner:catch (int i)" << Endl; throw i; } catch (...) {cout << "inner:catch (...)" << Endl; Throw }} catch (...) {cout << "outer:catch (...)" << Endl; }}/* hypothesis: The function in the current function-type third-party library, therefore, we cannot modify the source code function name: void func (int i) throw exception type: int-1 = = "parameter exception -2 = = "Run exception-3 = =" Timeout exception */void func (int i) {if (I < 0) {throw -1; } if (i >) {throw-2; } if (i = =) {throw-3; } cout << "Run func ..." << Endl;} The re-definition of the exception void MyFunc (int i) {try {func (i); } catch (int i) {switch (i) {case-1: Throw "Invalid Parameter"; Break Case-2: Throw "Runtime Exception"; Break Case-3: Throw "Timeout Exception"; Break }}}int Main (int argc, char *argv[]) {//Demo (); try {MyFunc (11); } catch (const char* CS) {cout << "Exception Info:" << cs << Endl; } return 0;}
Catch that matches the parent exception must be placed last, otherwise the function that catches the subclass exception will never be executed
#include <iostream> #include <string>using namespace std;class base{};class exception:public base{int m_i D string m_desc;public:exception (int id, string desc) {m_id = ID; M_DESC = desc; } int ID () const {return m_id; } string Description () const {return m_desc; }};/* hypothesis: The function in the current function-type third-party library, therefore, we cannot modify the source code function name: void func (int i) throw exception type: int-1 = = "parameter exception -2 = = "Run exception-3 = =" Timeout exception */void func (int i) {if (I < 0) {thro W-1; } if (i >) {throw-2; } if (i = =) {throw-3; } cout << "Run func ..." << Endl;} void MyFunc (int i) {try {func (i); } catch (int i) {switch (i) {case-1: Throw Exception ( -1, "Invalid Parameter "); Break Case-2: Throw ExceptIon ( -2, "Runtime Exception"); Break Case-3: Throw Exception ( -3, "Timeout Exception"); Break }}}int Main (int argc, char *argv[]) {try {MyFunc (11); }//reference as a parameter, you can improve the efficiency of the execution of the program, should be a copy of the class type of the constructor to produce a formal parameter//Direct pass reference can skip the copy construction, improve efficiency catch (const exception& e) {cout << "Exception Info:" << Endl; cout << "ID:" << e.id () << Endl; cout << "Description:" << e.description () << Endl; }//Assignment compatibility principle is applicable in exception matching, subclass exception is suitable for parent class exception! Exception capture of the parent class in engineering development at the end, the exception captures for subclasses are placed in the top catch (const base& e) {cout << catch (const base& e) << E Ndl } return 0;}
Reference as a parameter, you can improve the execution efficiency of the program, should be a copy of the class type of the process to produce a formal parameter //Direct pass reference can skip copy construction, improve efficiency
C + + exception handling