舉例說明自定義C++異常處理的實例
舉例說明自定義C++異常處理的實例
例1:自定義一個繼承自excepton的異常類myException
C++標準中,定義在<stdexcept>中的任何異常類都派生自exception Class,本例也只是簡單地由exception繼承,在try段拋出一個異常并捕捉。代碼如下:
/*++ test.cpp version:1.0 decript:define a exception class named myException derived from base class exception which is declared in <exception> created:2011-08-14 author: btwsmile --*/ #include<exception> #include<iostream> using namespace std; //customized exception class 'myException' class myException:public exception { public: myException():exception("ERROR! Don't divide a number by integer zero.\n") { } }; //entry of the application int main() { int x=100,y=0; try { if(y==0) throw myException(); else cout<<x/y; } catch(myException& me) { cout<<me.what(); } system("pause"); return 0; }
結果如下:
ERROR! Don't divide a number by integer zero.
請按任意鍵繼續(xù). . .
顯然,異常被捕捉到了。此處需要說明的是,VC對異常處理類exception進行了擴展,本例之所以能夠使用exception("ERROR!....")的初始化方法正出于這樣的原因,C++標準是不允許這樣做的。
與此同時,VC又沒有遵循標準,有力地支持terminate和unexpected,它只保留了語法,卻在編譯運行時不提供支持。為了結合terminate和unexpected更加深入了解C++的異常處理,下面的例子采用Dev cpp IDE實現(xiàn)。
例2:依照C++標準實現(xiàn)自定義異常類myException并將throw語句封裝到函數(shù)check()中涉及到的更改正如標題所述,(1)重寫基類的what()函數(shù),返回錯誤信息;(2)將throw myException()封裝到check()函數(shù)中;(3)允許check()函數(shù)拋出myException類型的異常。代碼如下:
/*++ test.cpp version:1.1 decript:define a exception class named myException according to C++ standard, derived from base class exception which is declared in <exception> !also,encapusulate throw into a function created:2011-08-14 author: btwsmile --*/ #include<exception> #include<iostream> using namespace std; //customized exception class 'myException' class myException:public exception { public: const char* what()const throw()//#1 { return "ERROR! Don't divide a number by integer zero.\n"; } }; void check(int y) throw(myException)//#2 { if(y==0) throw myException(); } //entry of the application int main() { int x=100,y=0; try { check(y); cout<<x/y; } catch(myException& me) { cout<<me.what(); } system("pause"); return 0; }
結果與例1完全相同。需說明的是,緊跟check()后的throw列表表明允許該函數(shù)拋出的異常類型。這里不得不產生疑問,如果拋出了一個不被允許的異常類型將怎樣?
例3:拋出unexpected異常
check函數(shù)體之后的throw列表,規(guī)定了允許拋出的異常類型,一旦違背,就將觸發(fā)unexpected。可以把unexpected看作系統(tǒng)自動調用的CALLBACK函數(shù),不同的是,也可以手工觸發(fā)它的執(zhí)行。本例的情況屬于前者。代碼如下:
/*++ test.cpp version:1.3 decript:define an unexpected excption handler, set it by using set_unexpected, modify the throw list of function check created:2011-08-14 author: btwsmile --*/ #include<exception> #include<iostream> using namespace std; //customized exception class 'myException' class myException:public exception { public: const char* what()const throw() { return "ERROR! Don't divide a number by integer zero.\n"; } }; void check(int y) throw()//#1 only int-type exception is permitted { if(y==0) throw myException(); } void myUnexpected() { cout<<"Unexpected exception caught!\n"; system("pause"); exit(-1); } //entry of the application int main() { unexpected_handler oldHandler=set_unexpected(myUnexpected); int x=100,y=0; try { check(y); cout<<x/y; } catch(myException& me) { cout<<me.what(); } system("pause"); return 0; }
結果如下:
Unexpected exception caught!
請按任意鍵繼續(xù). . .
check函數(shù)的throw列表為空,即不允許拋出任何類型的異常,然而實際上當異常發(fā)生時,系統(tǒng)不能等閑視之,它將調用unexpected處理方法。所以,限定一個函數(shù)throw列表為空是值得程序員警醒的事,需要特別留意。如果將#1處的代碼修改為throw(int)等也能得到相同的結果。所謂unexpected異常,說白了就是函數(shù)體允許拋出異常類型范圍之外的異常。如果check函數(shù)后面根本沒有throw,則表示函數(shù)任何類型的異常都被允許。
例4:拋出函數(shù)體允許的異常,但沒被捕捉到的情況
思考這樣一個問題,如果函數(shù)check的throw列表中有異常類型myException,而且在y==0時,它的確拋出myException類型的異常,但是沒有被catch到,這時會發(fā)生什么?
在正式回答這個問題之前,先討論“沒被catch到”的意思。比如,修改例3的代碼如下:(##為修改之處)
/*++ test.cpp version:1.4.1 decript: how to understand "exception not caucht"? created:2011-08-14 author: btwsmile --*/ #include<exception> #include<iostream> using namespace std; //customized exception class 'myException' class myException:public exception { public: const char* what()const throw() { return "ERROR! Don't divide a number by integer zero.\n"; } }; void check(int y) //any type of exception is permitted { if(y==0) throw myException(); } void myUnexpected() { cout<<"Unexpected exception caught!\n"; system("pause"); exit(-1); } //entry of the application int main() { unexpected_handler oldHandler=set_unexpected(myUnexpected); int x=100,y=0; try { check(y); cout<<x/y; } catch(int &e) //##1 no catch sentence matches the throw type { cout<<e<<endl; } /* ##2 if add this part, any type which's not handler before will be caught catch(...) { cout<<"Unkown exception caught!\n"; } */ system("pause"); return 0; }
編譯運行,程序將會出錯,因為check函數(shù)拋出的myException異常沒有被處理。在缺省情況下,一旦出現(xiàn)拋出異常沒被處理的問題,系統(tǒng)將自動調用abort()函數(shù),終止程序允許,在控制臺將會看到這樣的提示:
This application has requested the Runtime to terminate it in an unusual way.Please contact the application's support team for more information.
不過可以增加##2部分的代碼,catch(...)表示捕捉任何類型的異常。
注意:check函數(shù)不被允許的異常類型并不會進入到catch語句的判斷中來,因此catch(...)對unexpected exception沒有作用。
仍然考慮沒有##2部分的情況。正如前面所述,系統(tǒng)將自動調用abort()函數(shù)終止程序。實際上,它觸發(fā)的是terminate,類似于unexpected,仍然可以自定義terminate的處理方法。甚至terminate語法上跟unexpected都十分近似。修改代碼為:
/*++ test.cpp version:1.4.2 decript: how to understand "exception not caucht"? created:2011-08-14 author: btwsmile --*/ #include<exception> #include<iostream> using namespace std; //customized exception class 'myException' class myException:public exception { public: const char* what()const throw() { return "ERROR! Don't divide a number by integer zero.\n"; } }; void check(int y) //any type of exception is permitted { if(y==0) throw myException(); } void myUnexpected() { cout<<"Unexpected exception caught!\n"; system("pause"); exit(-1); } void myTerminate() //##1 set it be the terminate handler { cout<<"Unhandler exception!\n"; system("pause"); exit(-1); } //entry of the application int main() { unexpected_handler oldHandler=set_unexpected(myUnexpected); terminate_handler preHandler=set_terminate(myTerminate); int x=100,y=0; try { check(y); cout<<x/y; } catch(int &e) //no catch sentence matches the throw type { cout<<e<<endl; } system("pause"); return 0; }
結果如下:
Unhandler exception!
請按任意鍵繼續(xù). . .
結論:C++為異常處理提供了友好的支持。
用戶可以自定義異常類型,異常類型并不受到限制,可以是內建數(shù)據(jù)類型如int,double等,也可以是自定義的類,也可以從C++某個異常類繼承下來。例1采用了派生自exception的方法。
除此之外,在定義函數(shù)時,可以顯式指定函數(shù)體拋出的異常類型。隱式情況下,缺省允許函數(shù)拋出任何類型的異常。有可以增加throw語句,對異常類型加以限制。特別的是,throw()表示不允許函數(shù)拋出任何類型的異常。如果違反了throw列表規(guī)定的異常類型,系統(tǒng)將調用unexpected hanlder進行處理,可以自定義unexpected異常處理方法。例2和例3對它們進行了說明。
如果對于函數(shù)體throw列表合法的異常被拋出,但是卻沒有被程序捕捉處理,系統(tǒng)將調用terminate handler進行處理。缺省情況下,只是簡單調用abort()函數(shù)終止程序,同樣可以自定義terminate處理方法。例4對它進行了說明。
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關鍵字的使用詳解
- 01-10深入C/C++浮點數(shù)在內存中的存儲方式詳解
- 01-10深入理解C/C++混合編程


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery