舉例說明自定義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ù)拋出的異常類型。這里不得不產(chǎn)生疑問,如果拋出了一個不被允許的異常類型將怎樣?
例3:拋出unexpected異常
check函數(shù)體之后的throw列表,規(guī)定了允許拋出的異常類型,一旦違背,就將觸發(fā)unexpected??梢园製nexpected看作系統(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ū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
利用C語言模擬實現(xiàn)qsort,strcpy,strcat,strcmp函數(shù)
這篇文章主要為大家詳細介紹了如何通過C語言模擬實現(xiàn)qsort(采用冒泡的方式),strcpy,strcat,strcmp等函數(shù),文中的示例代碼講解詳細,感興趣的可以了解一下2022-11-11
Qt6.3 + Clion +MSVC2019環(huán)境配置詳解
本文主要介紹了Qt6.3 + Clion +MSVC2019環(huán)境配置詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01
教你Visual?Studio?2022如何新建一個C語言工程(圖文詳解)
這篇文章主要介紹了Visual?Studio?2022如何新建一個C語言工程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09

