欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

舉例說明自定義C++異常處理的實(shí)例

 更新時(shí)間:2017年10月16日 10:11:53   作者:_QING_FENG  
這篇文章主要介紹了舉例說明自定義C++異常處理的實(shí)例的相關(guān)資料,這里舉例說明該如何使用C++ 的異常,需要的朋友可以參考下

舉例說明自定義C++異常處理的實(shí)例

例1:自定義一個(gè)繼承自excepton的異常類myException

C++標(biāo)準(zhǔn)中,定義在<stdexcept>中的任何異常類都派生自exception Class,本例也只是簡單地由exception繼承,在try段拋出一個(gè)異常并捕捉。代碼如下:

/*++ 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; 
} 

結(jié)果如下:

ERROR! Don't divide a number by integer zero.

請按任意鍵繼續(xù). . .                                                 

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

與此同時(shí),VC又沒有遵循標(biāo)準(zhǔn),有力地支持terminate和unexpected,它只保留了語法,卻在編譯運(yùn)行時(shí)不提供支持。為了結(jié)合terminate和unexpected更加深入了解C++的異常處理,下面的例子采用Dev cpp IDE實(shí)現(xiàn)。

例2:依照C++標(biāo)準(zhǔn)實(shí)現(xiàn)自定義異常類myException并將throw語句封裝到函數(shù)check()中涉及到的更改正如標(biāo)題所述,(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; 
} 

結(jié)果與例1完全相同。需說明的是,緊跟check()后的throw列表表明允許該函數(shù)拋出的異常類型。這里不得不產(chǎn)生疑問,如果拋出了一個(gè)不被允許的異常類型將怎樣?

例3:拋出unexpected異常

check函數(shù)體之后的throw列表,規(guī)定了允許拋出的異常類型,一旦違背,就將觸發(fā)unexpected??梢园製nexpected看作系統(tǒng)自動調(diào)用的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; 
} 

結(jié)果如下:

Unexpected exception caught!

請按任意鍵繼續(xù). . .                   

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

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

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

在正式回答這個(gè)問題之前,先討論“沒被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; 
} 

編譯運(yùn)行,程序?qū)鲥e,因?yàn)閏heck函數(shù)拋出的myException異常沒有被處理。在缺省情況下,一旦出現(xiàn)拋出異常沒被處理的問題,系統(tǒng)將自動調(diào)用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ù)不被允許的異常類型并不會進(jìn)入到catch語句的判斷中來,因此catch(...)對unexpected exception沒有作用。
仍然考慮沒有##2部分的情況。正如前面所述,系統(tǒng)將自動調(diào)用abort()函數(shù)終止程序。實(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; 
} 

結(jié)果如下:

Unhandler exception!

請按任意鍵繼續(xù). . .   

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

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

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

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

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

相關(guān)文章

  • C++?引用與內(nèi)聯(lián)函數(shù)詳情

    C++?引用與內(nèi)聯(lián)函數(shù)詳情

    這篇文章主要介紹了C++?引用與內(nèi)聯(lián)函數(shù)詳情,主要分享一下關(guān)于引用的知識點(diǎn),這里都是一些比較基礎(chǔ)的知識,適合初學(xué)者,下文續(xù)航徐介紹需要的小伙伴可以參考一下
    2022-05-05
  • 基于C語言實(shí)現(xiàn)簡單掃雷游戲

    基于C語言實(shí)現(xiàn)簡單掃雷游戲

    這篇文章主要為大家詳細(xì)介紹了基于C語言實(shí)現(xiàn)簡單掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • C語言實(shí)現(xiàn)帶頭雙向循環(huán)鏈表的接口

    C語言實(shí)現(xiàn)帶頭雙向循環(huán)鏈表的接口

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)帶頭雙向循環(huán)鏈表的接口,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • 詳解C/C++如何發(fā)送與接收Kafka消息

    詳解C/C++如何發(fā)送與接收Kafka消息

    系統(tǒng)之間通信方式很多如:系統(tǒng)之間調(diào)用(http/rpc等),異步間接調(diào)用如發(fā)送消息、公共存儲等,算法工程為C/C++工程,本文將介紹如何在C/C++中如何發(fā)送與接收Kakfa消息(包含:Kafka的SASL認(rèn)證方式),并提供了詳細(xì)的源碼和講解,需要的朋友可以參考下
    2024-07-07
  • 利用C語言模擬實(shí)現(xiàn)qsort,strcpy,strcat,strcmp函數(shù)

    利用C語言模擬實(shí)現(xiàn)qsort,strcpy,strcat,strcmp函數(shù)

    這篇文章主要為大家詳細(xì)介紹了如何通過C語言模擬實(shí)現(xiàn)qsort(采用冒泡的方式),strcpy,strcat,strcmp等函數(shù),文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2022-11-11
  • Qt6.3 + Clion +MSVC2019環(huán)境配置詳解

    Qt6.3 + Clion +MSVC2019環(huán)境配置詳解

    本文主要介紹了Qt6.3 + Clion +MSVC2019環(huán)境配置詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Linux UDP服務(wù)端和客戶端程序的實(shí)現(xiàn)

    Linux UDP服務(wù)端和客戶端程序的實(shí)現(xiàn)

    這篇文章主要介紹了Linux UDP服務(wù)端和客戶端程序的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • 教你Visual?Studio?2022如何新建一個(gè)C語言工程(圖文詳解)

    教你Visual?Studio?2022如何新建一個(gè)C語言工程(圖文詳解)

    這篇文章主要介紹了Visual?Studio?2022如何新建一個(gè)C語言工程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-09-09
  • OpenCV實(shí)現(xiàn)圖像距離變換

    OpenCV實(shí)現(xiàn)圖像距離變換

    這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)圖像距離變換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Define,const,static用法總結(jié)

    Define,const,static用法總結(jié)

    const定義的全局?jǐn)?shù)據(jù)變量,其基本作用和define相同,但又在define的基礎(chǔ)上增加了好多功能
    2013-10-10

最新評論