#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;//拋出異常 } return ret; } int main(int argc, char *argv[]) { try { double r = divide(1, 0);//divide除數爲0時拋出異常,若是不try catch 程序會異常終止 cout << "r = " << r << endl; } catch(...)//...表示捕獲任何類型的異常 這裏divide得異常時 int類型的 { cout << "Divided by zero..." << endl; } return 0; }
#include <iostream> #include <string> using namespace std; //try 中捕獲的異常類型必須跟 cath中的異常類型嚴格匹配!不進行任何類型轉換 //類類型的異常 適用賦值兼容性原則,子類的異常會匹配父類的catch語句 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(...)//...語句必須放在最後,捕捉未被catch的任意類型的異常 { cout << "catch(...)" << endl; } } void Demo2() { throw string("D.T.Software");//const char *cs類型 } 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; }
catch語句拋出異常,目的是爲了從新定義異常信息:ios
#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; } } /* 假設: 當前的函數式第三方庫中的函數,所以,咱們沒法修改源代碼 函數名: void func(int i) 拋出異常的類型: int -1 ==》 參數異常 -2 ==》 運行異常 -3 ==》 超時異常 */ void func(int i) { if( i < 0 ) { throw -1; } if( i > 100 ) { throw -2; } if( i == 11 ) { throw -3; } cout << "Run func..." << endl; } //異常的從新定義 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必須放在最後,不然捕獲子類異常的函數將永遠不會被執行ide
#include <iostream> #include <string> using namespace std; class Base { }; class Exception : public Base { int m_id; 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; } }; /* 假設: 當前的函數式第三方庫中的函數,所以,咱們沒法修改源代碼 函數名: void func(int i) 拋出異常的類型: int -1 ==》 參數異常 -2 ==》 運行異常 -3 ==》 超時異常 */ void func(int i) { if( i < 0 ) { throw -1; } if( i > 100 ) { throw -2; } if( i == 11 ) { 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); } //引用做爲參數,能夠提升程序的執行效率,應爲類類型的實參會通過拷貝構造產生一個形參 //直接傳遞引用能夠跳過拷貝構造,提升效率 catch(const Exception& e) { cout << "Exception Info: " << endl; cout << " ID: " << e.id() << endl; cout << " Description: " << e.description() << endl; } //賦值兼容性原則在異常匹配中是適用的,子類異常適合父類異常! //工程開發中父類的異常捕獲放在最後,子類的異常捕獲放在最前 catch(const Base& e) { cout << "catch(const Base& e)" << endl; } return 0; }
//引用做爲參數,能夠提升程序的執行效率,應爲類類型的實參會通過拷貝構造產生一個形參 //直接傳遞引用能夠跳過拷貝構造,提升效率