国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

舉例說明自定義C++異常處理的實例

2020-01-26 13:52:50
字體:
來源:轉載
供稿:網友

舉例說明自定義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.

請按任意鍵繼續. . .                                                 

顯然,異常被捕捉到了。此處需要說明的是,VC對異常處理類exception進行了擴展,本例之所以能夠使用exception("ERROR!....")的初始化方法正出于這樣的原因,C++標準是不允許這樣做的。

與此同時,VC又沒有遵循標準,有力地支持terminate和unexpected,它只保留了語法,卻在編譯運行時不提供支持。為了結合terminate和unexpected更加深入了解C++的異常處理,下面的例子采用Dev cpp IDE實現。

例2:依照C++標準實現自定義異常類myException并將throw語句封裝到函數check()中涉及到的更改正如標題所述,(1)重寫基類的what()函數,返回錯誤信息;(2)將throw myException()封裝到check()函數中;(3)允許check()函數拋出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列表表明允許該函數拋出的異常類型。這里不得不產生疑問,如果拋出了一個不被允許的異常類型將怎樣?

例3:拋出unexpected異常

check函數體之后的throw列表,規定了允許拋出的異常類型,一旦違背,就將觸發unexpected。可以把unexpected看作系統自動調用的CALLBACK函數,不同的是,也可以手工觸發它的執行。本例的情況屬于前者。代碼如下:

 /*++ 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!

請按任意鍵繼續. . .                   

check函數的throw列表為空,即不允許拋出任何類型的異常,然而實際上當異常發生時,系統不能等閑視之,它將調用unexpected處理方法。所以,限定一個函數throw列表為空是值得程序員警醒的事,需要特別留意。如果將#1處的代碼修改為throw(int)等也能得到相同的結果。所謂unexpected異常,說白了就是函數體允許拋出異常類型范圍之外的異常。如果check函數后面根本沒有throw,則表示函數任何類型的異常都被允許。

例4:拋出函數體允許的異常,但沒被捕捉到的情況

思考這樣一個問題,如果函數check的throw列表中有異常類型myException,而且在y==0時,它的確拋出myException類型的異常,但是沒有被catch到,這時會發生什么?

在正式回答這個問題之前,先討論“沒被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函數拋出的myException異常沒有被處理。在缺省情況下,一旦出現拋出異常沒被處理的問題,系統將自動調用abort()函數,終止程序允許,在控制臺將會看到這樣的提示:

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函數不被允許的異常類型并不會進入到catch語句的判斷中來,因此catch(...)對unexpected exception沒有作用。
仍然考慮沒有##2部分的情況。正如前面所述,系統將自動調用abort()函數終止程序。實際上,它觸發的是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!

請按任意鍵繼續. . .   

結論:C++為異常處理提供了友好的支持。

用戶可以自定義異常類型,異常類型并不受到限制,可以是內建數據類型如int,double等,也可以是自定義的類,也可以從C++某個異常類繼承下來。例1采用了派生自exception的方法。

除此之外,在定義函數時,可以顯式指定函數體拋出的異常類型。隱式情況下,缺省允許函數拋出任何類型的異常。有可以增加throw語句,對異常類型加以限制。特別的是,throw()表示不允許函數拋出任何類型的異常。如果違反了throw列表規定的異常類型,系統將調用unexpected hanlder進行處理,可以自定義unexpected異常處理方法。例2和例3對它們進行了說明。

如果對于函數體throw列表合法的異常被拋出,但是卻沒有被程序捕捉處理,系統將調用terminate handler進行處理。缺省情況下,只是簡單調用abort()函數終止程序,同樣可以自定義terminate處理方法。例4對它進行了說明。

如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 建昌县| 江源县| 巧家县| 阳高县| 盐源县| 苗栗县| 福州市| 广饶县| 莆田市| 灵川县| 多伦县| 马边| 山阴县| 黔江区| 如东县| 龙南县| 德安县| 读书| 安西县| 嘉鱼县| 台湾省| 汉寿县| 隆德县| 友谊县| 五大连池市| 辽阳县| 温宿县| 灵武市| 乳源| 大邑县| 大石桥市| 新营市| 当雄县| 江津市| 循化| 兴海县| 全椒县| 巴塘县| 武汉市| 高阳县| 班玛县|