版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、第七章異常及其處理(exception and its handling7.1程序運行錯誤及其處理程序在運行中可能出錯,例如在除法運算中用0作除數(shù)、求負數(shù)的開方根、在計算機監(jiān)控系統(tǒng)中所采集的數(shù)據(jù)越限,等等。當程序出錯時,一般要求用戶進行 處理,如重新輸入數(shù)值或給出新的控制量等。必要時則中斷程序。程序首先應檢 測到出錯,然后才能提示用戶進行處理或由程序自動進行處理。傳統(tǒng)的錯誤處理方式唯有依靠函數(shù)返回提示錯誤用的局部標志數(shù)據(jù)或者全局 標志數(shù)據(jù)來提示出錯(全局數(shù)據(jù)的安全性差,極易受到破壞)。這類做法的困難是 程序只能在函數(shù)返回主程序后才能檢查標志數(shù)據(jù)。錯誤處理的一種新的方式是使用“異?!?excep
2、tion)。異常處理是由程序設計 語言提供的運行時刻錯誤處理的一種方式。一旦程序出現(xiàn)錯誤,隨即引發(fā)和拋出“異?!?,程序能在一處或多處合適的地方自動地捕獲異常并進行必要的處理。例1求負數(shù)的開方根時傳統(tǒng)的錯誤處理方式:/ sqrt_negative_1.cpp/ error occurs when the square root of a negative number is calculated/ system subroutine used#include <iostream.h>#include <math.h> / ptototype: double sqrt( d
3、ouble );#include <stdlib.h> / ptototype: void exit(int);double sqroot(double number)if ( number < 0 )cout << "Error! negative input number : " << number << 'n'cout << "Program terminated!" << 'n'exit( -1 );return sqrt( number
4、 ); /system subroutinevoid main()cout<<"Sqrt of 1.5129 is "<<sqroot(1.5129)<<endl;cout<<"Sqrt of -4 is "<<sqroot(-4)<<endl;cout<<"Sqrt of 16 is "<<sqroot(16)<<endl;/* Results:Sqrt of 1.5129 is 1.23Error! negative inp
5、ut number : -4Program terminated!*/以上程序中,當準備求取負數(shù)的平方根時,就出錯,程序非正常結束。為使程序非正常結束,須要調用系統(tǒng)子程序exit( ),其函數(shù)原型在頭文件stdlib.h 內。其中主函數(shù)中第二句運行過程中,因調用函數(shù)sqroot(-4)B導致程序意外中斷。但在sqroot(-4)前的字符串"Sqrt of-4 is ”卻沒有顯示。這類現(xiàn)象將在以后眾多程 序中出現(xiàn)。它是由于輸出語句的整體性所決定的。 請見第八章§ 8.1.2.2“非緩沖輸出流”中的詳細解釋。例 2第五章§ 5.3.1.3“除法運算符重載”中的例 1該
6、程序中的子程序double division:operator / ( double denominator ) if ( denominator = 0 ) cout<<"attempted to divide by zero"<<endl; return -999999;return (numerator/denominator); 將-999999 的返回值作為錯誤標志。 void main() double numerator, denominator;double result;division num(numerator);result
7、= num/denominator;/i.e. result=num. operator / ( denominator )if ( result = -999999 ) cout << "The quotient is meaningless!" << endl;cout << "Try again!" << endl;主函數(shù) main( )在子函數(shù) double division:operator / ( double denominator )返回主函數(shù)后立即檢查返回值,如發(fā)現(xiàn)出錯,就加以顯示。如果程
8、序中出錯地方較多,這類處理方法就有其局限性:當調用函數(shù)的級聯(lián)數(shù)量較多(即一個函數(shù)調用下一個函數(shù),而下一個又調用再下一個的級聯(lián)方式)而又需要在其它地方(例如上級的上級的上級)統(tǒng)一進行處理時,逐級傳遞錯誤碼太繁瑣。因此須要借助于異常處理。請參閱后面本章§ 7.2.1 “拋出異常”中的例 3中的程序exception_5.cpp。 例 3 依靠異常來處理求負數(shù)開方根的例子 (當然此例中并不一定需要異常處理) :/ exception_sqrt_1.cpp/ exception occurs when the square root of a negative number is calcu
9、lated#include <iostream.h>#include <math.h> / ptototype: double sqrt( double );/#include <stdlib.h> /不再需要 exit( )double sqroot(double number)if ( number < 0 ) throw number; /exception objectreturn sqrt( number ); /system subroutine void main() try cout<<"Sqrt of 1.512
10、9 is "<<sqroot(1.5129)<<endl;cout<<"Sqrt of -4 is "<<sqroot(-4)<<endl;cout<<"Sqrt of 16 is "<<sqroot(16)<<endl;catch ( double ) / exception handlercout << "Error! negative input number" << 'n'cout
11、<< "Program terminated!" << 'n'/exit( -1 );/因已到程序結尾,故不再需要此函數(shù)/* Results:Sqrt of 1.5129 is 1.23Error! negative input number Program terminated!*/一種較好的編程方式是首先編寫正常運行的代碼,然后再添加用于處理 各類異常情況的語句。7.2 異常及其處理7.2.1 拋出異常從上節(jié)例2中可以看出,當監(jiān)測到程序出錯時,就拋出異常。其格式為: try監(jiān)測到(或在被調用的程序中監(jiān)測到)程序出錯時, throw
12、異常對象;其中被拋出的異常對象可以是預定義數(shù)據(jù)類型(例如int、double char*等)的變量及其指針和引用,但更經常使用的是各種異常類的對象及其指針和引用。使用這些異常類對象的優(yōu)點是能夠提供更多關于程序錯誤的信息,包括處理各類 錯誤的方法。這些異常類可以是系統(tǒng)所提供的.更多情況下可以是用戶自己定義應該注意:此處要求一定在try程序塊中或它所調用的函數(shù)中拋出異常。以下是不在try程序塊中而在try程序塊所調用的函數(shù)中拋出異常的例子:例1在try程序塊所調用的函數(shù)quotient()中拋出異常的例子/ exception_1.cpp/ A simple exception handling
13、example./ Checking for a divide-by-zero exception/ and throwing an intetger as an exception#include <iostream.h>/ Definition of function quotient. Demonstrates throwing/ an exception when a divide-by-zero exception is encountered. double quotient( double numerator, double divisor )if ( divisor
14、 = 0 ) throw divisor;被拋出的異常對象是預定義類型即double型數(shù)據(jù)return numerator / divisor;/ Driver programvoid main()/ the try block wraps the code that may throw an/ exception and the code that should not execute/ if an exception occurstry cout << "The quotient of 18/9 is " << quotient ( 18, 9
15、) << endl;cout << "The quotient of 18/0 is " << quotient ( 18, 0 ) << endl;cout << "The quotient of 4.5/9 is " << quotient ( 4.5, 9 ) << endl;catch ( double dd ) / exception handlercout << "Exception : divisor " << d
16、d << " used!" << endl;/* Results:The quotient of 18/9 is 2Exception : divisor 0 used!*/從以上例子中可以看出,當程序不出錯時,程序按照順序控制結構逐條語句 地向下執(zhí)行,并返回除法運算結果。而當程序出錯時(即當除數(shù)為零時) ,程序就 在拋出異常處中斷,不再執(zhí)行以后的語句(不執(zhí)行第三句),并跳轉至catch程序 塊再繼續(xù)往下執(zhí)行程序。例2使用系統(tǒng)的異常類class exception為止匕,須包含頭文件exception,/ exception_3.cpp/ same
17、as exception_1.cpp, only differing in/ throwing an object of a class instead of a double value#include <iostream.h>#includeexception,/ Definition of function quotient. Demonstrates throwing/ an exception when a divide-by-zero exception is encountered.double quotient( double numerator, double d
18、ivisor )if ( divisor = 0 ) throw exception();被拋出的異常對象是異常類即 class exception勺對象,/class exception is declared in the include file <exception> return numerator / divisor;/ Driver programvoid main()try cout << "The quotient of 18/9 is " << quotient (18, 9 ) << endl;cout
19、<< "The quotient of 18/0 is " << quotient ( 18, 0 ) << endl;cout << "The quotient of 4.5/9 is " << quotient ( 4.5, 9 ) << endl;catch ( exception ex ) / exception handler cout << ex.what( ) << 'n'/* Results:The quotient of 18
20、/9 is 2Unknown exception */以上程序中class exceptions系統(tǒng)所提供的異常類,在拋出異常時建立該異常 類exception的對象。為使用class exception在程序中應包含頭文件exception,, 在§7.3中將會詳細介紹它。catch程序塊中內容將在下一小節(jié)§7.2.2中介紹。以上程序中,當出現(xiàn)異常時,拋出異常,程序流即自動退出 try程序塊,進入 catch程序塊,進行處理后即進入 catch程序塊以后的語句,不再繼續(xù)執(zhí)行 try程 序塊中拋出異常語句后的其他語句(上例中不再執(zhí)行第三條語句“ quotient (4.5,
21、 9 )”)。以上程序中無法顯示異常內容,但只需將以上程序略加修改,即可顯示異常 內容。如exception_4.cpp(未顯示整個程序)中,只需將 double quotient( int numerator, int divisor )函數(shù)的第 旬“ if ( divisor = 0 )throw exceptio,); ”改為:“if ( divisor = 0 ) throw exceptio, "Divide-by-zero exception");使用系統(tǒng)的異常類的對象并初始化”即可!也就是在建立 class exception的對象時,使用字符串"d
22、ivide-by-zero exception,將該異常對象的字符串數(shù)據(jù)初始化,輸入用于顯示異常性質的字符串。在調用異常對象中的 what()函數(shù)時就能顯示該字符串。這就獲得如下更明確的運行結果:/* Results:The quotient of 18/9 is 2Divide-by-zero exception*/以前提到過,當函數(shù)級聯(lián)調用時,使用顯示出錯的函數(shù)返回值,要逐級向上 遞送,很不方便。而使用異常處理就方便了。例3函數(shù)級聯(lián)調用時的異常處理/ exception_5.cpp/ cascaded invocation of sub-routines#include <iostr
23、eam.h>#include <exception>/ Definition of function quotient. Demonstrates throwing/ an exception when a divide-by-zero exception is encountered. double quotient( double numerator, double divisor )if ( divisor = 0 ) throw exception( "Divide-by-zero exception");/使用系統(tǒng)的異常類return numer
24、ator / divisor;double h( double numerator, double divisor )double result = quotient( numerator, divisor );cout << "function h( ) in return path!" << endl;return result;double g( double numerator, double divisor )double result = h( numerator, divisor );cout << "functi
25、on g( ) in return path!" << endl;return result;double f( double numerator, double divisor )double result = g( numerator, divisor );cout << "function f( ) in return path!" << endl;return result;/ Driver programvoid main()try cout << "The quotient of 18/9 is
26、 " << f ( 18, 9 ) << endl;cout << "The quotient of 18/0 is " << f ( 18, 0 ) << endl;cout << "The quotient of 4.5/9 is " << f ( 4.5, 9 ) << endl;catch ( exception ex ) / exception handlercout << ex.what( ) << 'n&
27、#39;/* Results:function h( ) in return path!function g( ) in return path!function f( ) in return path!The quotient of 18/9 is 2Divide-by-zero exception*/以上程序中正常調用函數(shù)的路徑是: main( ) -> f( ) -> g( ) -> h( ) ->quotient( ) , 在函數(shù)返回時則采取相反路徑,即 quotient( ) -> h( ) -> g( ) -> f( ) -> mai
28、n( ) 。但在出現(xiàn)異常時,也即調用主函數(shù)中以下語句cout << "The quotient of 18/0 is " << f ( 18, 0 ) << endl;時,就不再按步就班,而是直接從 quotient (泅回至main()中的catch程序塊。其流程如下圖所示:其中正常運行流程引發(fā)異常流程表示函數(shù)調用路徑*表示正常返回路徑- 表示出現(xiàn)異常時的返回路徑7.2.2 捕獲異常捕獲異常以便進行處理時,通常使用 catch程序塊,其格式為:catch (數(shù)據(jù)或類的對象)程序塊可在該程序塊中顯示相關信息和進行必要處理。例1使用異常檢查
29、整型數(shù)組下標越限/ exception_7.cpp/使用異常后查整型數(shù)組下標越限#include <iostream.h>int arr = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10;int LEN; / number of elementsdouble f(int i)if ( (i>=LEN) | (i<0)throw i;return arri;void main()/ Number of elements is determined firstLEN = sizeof(arr)/sizeof(arr0);try cout<<&quo
30、t;arr1="<<f(1)<<endl;cout<<"arr4="<<f(4)<<endl;cout<<"arr-2="<<f(-2)<<endl;catch (int x) cout << "Exception:subscript "<< x<<" beyond limits (number of elements = "<<LEN<<"
31、)" << endl;/* Results:arr1=2arr4=5Exception:subscript -2 beyond limits (number of elements = 10)*/以上程序中,主程序首先自動地確定數(shù)組單元數(shù)。無論該數(shù)組類型如何,都可正確地求得。當下標越限時,就拋出異常。稍后在catch 程序塊中捕獲異常,并顯示該下標的數(shù)值。以上是捕獲一個異常的例子,以下是捕獲兩個異常并作不同顯示的例子: 例 2 輸入兩個數(shù)值,兩者相除,求其商。如分母為零,拋出第一類異常。如錯誤地輸入錯誤類型的變量例如字符變量,則拋出第二類異常。這兩類異常由一個catch
32、程序塊捕獲,而能作不同顯示處理。/ exception_6.cpp/ different exceptions caught by one block#include <iostream.h>#include <exception>double quotient( double numerator, double divisor )if ( divisor = 0 )throw exception( "Divide-by-zero exception" );/使用系統(tǒng)的異常類并初始化其對象return numerator / divisor;void
33、 main()double numerator, divisor;cout << "Enter two values: "cin >> numerator >> divisor;try if (!cin)throw exception( "Wrong input type exception" );cout << "The quotient of "<<numerator<<'/'<<divisor<<" is &
34、quot;<< quotient ( numerator, divisor ) << endl;cout << "The quotient of 4.5/9 is " << quotient ( 4.5, 9 ) << endl;catch ( exception ex ) / exception handler cout << ex.what( ) << 'n'/* Three kinds of results:(1)Enter two values: 18 9The qu
35、otient of 18/9 is 2The quotient of 4.5/9 is 0.5(2)Enter two values: 18 0Divide-by-zero exception(3)Enter two values: 18 pWrong input type exception*/上例中如分母為零,拋出一個異常類對象。如錯誤地輸入字符變量,則拋出另一 個異常類對象。這兩類異常類對象的差別在于其初始化字符串內容不同。它們都能 由同一個catch程序塊捕獲,進行處理,顯示不同內容:對第一個異常類對象顯示“ Divideby-zero exception ; 對另 個異常 類對象 貝
36、 顯示 “Wronginput type exception :'程序中(!cin)是“operator!("個重載運算符函數(shù))和“cin”(一個輸入流對象)的組合。當輸入操作成功,(!cin)返回零值;如輸入操作失敗,則(!cin)返回非零值。(!cin)的功能詳見第八章 B1.2.4 緩沖輸入流”例5后的解釋。附錄十九是多個地方拋出的相同內容的異常在同一個catch程序塊內處理的另一個例子。還有一種格式catch ()可用于捕獲各類異常,或用于捕獲其它catch ()格式所不能捕獲的異常。有興趣者可參閱附錄二十。C+異異常處理機制可簡敘如下:1 .如果try塊中沒有拋出異
37、常,則所有與try塊相關的catch塊將被忽略,程序跳過 所有catch塊后繼續(xù)運行。2 . catch塊一般位于try塊之后。如果try塊中拋出異常,try塊中的剩余語句將被跳 過而忽略。程序轉至其參數(shù)類型與異常類型相匹配的catch塊中,由其處理該異常。執(zhí)行完畢后,跳過其它catch塊,再執(zhí)行所有catch塊以后的其它語句。3 .如果沒有其參數(shù)類型與異常類型相匹配的catch塊,則由catch ()塊處理該異常。7.3 用干處理異常的類7.3.1 系統(tǒng)提供的異常類exception_2.cp葉拋出異常的語句是:if ( divisor = 0 ) throw exception();而其運
38、行結果顯示異常性質為 Unknown exception而exception_3.cpp(未顯示整個程序)中拋出異常的語句是 :if ( divisor = 0 )throw exception( "divide-by-zero exception");而其運行結果顯示異常性質為divide-by-zero exception這是建立異常類的對象的兩種不同方式。這是什么異常類?先看一下系統(tǒng)的異常類。系統(tǒng)在頭文件exception (沒有exception.)中聲明了用于處理異常的類 exception該類的接口如下: class exception public:excep
39、tion。;exception(const char* &);exception(const exception&);exception& operator= (const exception&);virtual exception();virtual char* what( ) const; private:char* _m_what;int _m_doFree;一一exception_2.cpp中拋出異常的語句是:if ( divisor = 0 ) throw exception();它七建立對象時調用構造函數(shù) exception。,沒有任何初始化參數(shù)。而
40、字符 用“Unknown exceptions"則是該異常類構造函數(shù)的缺省參數(shù)。而exception_3.cp葉拋出異常的語句是:if ( divisor = 0 )throw exception( "divide-by-zero exception");它在建立對象時調用重載的構造函數(shù) exception ( const char* & ),將字符串 "divide-by-zero exception"用作初始化參數(shù), 寫入 exception : _m_what字符串 中。而以后主程序中調用該對象 ex的成員函數(shù)ex.what()時
41、,加在運行結果中 顯示該字符串,指出異常性質為divide-by-zero exceptions用戶除使用系統(tǒng)的異常類之外,也可自己定義該系統(tǒng)異常類class exception的派生類。例1用戶自己定義系統(tǒng)class exception勺派生異常類/ exception_derived_loop_1.cpp/ derived class of system class exception#include <iostream.h>#include <exception> double denominator;class Except_derive : public ex
42、ception一char *message; public:Except_derive(char * ptr) message = ptr; const char * what( ) const return message; void handling ( Xcout << "Enter denominator" (分母)again :"cin >> denominator; ;void main()double numerator;bool flag = true;cout << "Enter two data :
43、"cin >> numerator >> denominator;while ( flag )tryif ( denominator = 0 ) throw Except derive( "divide by zero!"); cout<< "The quotient is: " <<(numerator/denominator)<<endl;flag = false;catch ( Except derive ex ) / exception handlercout <<
44、 "Exception : " << ex.what( ) << 'n'ex.handling (); /while/* Results:Enter two data : 4 0Exception : divide by zero!Enter denominator" (分母)again : 8The quotient is: 0.5*/以上程序中使用了循環(huán)方式,當出現(xiàn)異常時可循環(huán)運行,以便要求用戶再次輸 入正確數(shù)據(jù)。只當輸入的兩個數(shù)據(jù)都正確時,程序才給出運算結果并正常結束。7.3.2 用戶自定義異常類上例只說明用戶可定義系
45、統(tǒng)異常類的派生類,但并無實際意義。還不如用戶 自己定義異常類。如下例:例1在給定溫度條件下進行除法運算。先檢查溫度,如溫度過高或過低,則調 整溫度至給定范圍。然后在除法運算中檢查零除數(shù)。異常類的基類用于處理零除 數(shù);異常類的派生類則用于處理溫度過高或溫度過低,并能自動地每次將溫度調 整10度。/ exception_derived_loop_2.cpp/ user-defined class except and its derived classes#include <iostream.h>#define MAX_T 28#define MIN_T 16int temperatu
46、re;double denominator;class exceptchar *message;public:except (char * ptr) message = ptr; const char * what( ) const return message; virtual void handling ( ) cout << "Enter a non-zero denominator again :"cin >> denominator; void action ( ) cout << "Exception : "
47、; << what( ) << 'n'handling" ;class except_derive : public except一public:except_derive(char * ptr) : except (ptr) virtual void handling ( ) if ( temperature > MAX_T )cout <<“啟動溫控系統(tǒng),將溫度降至 "<< (temperature -= 10) << endl; elsecout << “啟動溫控系統(tǒng),將溫度
48、升至"<< (temperature += 10) << endl;double quotient( double numerator, double denominator ) if ( denominator = 0 ) throw except( "divide by zero!");return numerator / denominator;void main() double numerator, result;bool flag = true;char * mes_low = "Temperature too low
49、!"char * mes_high = "Temperature too high!"cout << "Measured temperature :"cin >> temperature;cout << "Enter numerator and denominator :"cin >> numerator >> denominator;while ( flag ) try if ( temperature > MAX_T )|( temperature <
50、; MIN_T )throw except derive( temperature > MAX T )?( mes high ):( mes low ); result = quotient ( numerator, denominator );cout << "The quotient is: " << result << endl;flag = false;/ The following order should not be reversed catch ( except derive ex ) ex.action( ); c
51、atch ( except ex )ex.action( ); / while/* At least three kinds of results:(1)Measured temperature : 20Enter numerator and denominator : 2 4The quotient is: 0.5(2)Measured temperature : 40Enter numerator and denominator : 4 8Exception : Temperature too high!啟動溫控系統(tǒng),將溫度降至30Exception : Temperature too h
52、igh!啟動溫控系統(tǒng),將溫度降至20The quotient is: 0.5(3)Measured temperature : 5Enter numerator and denominator : 4 0Exception : Temperature too low!啟動溫控系統(tǒng),將溫度升至15Exception : Temperature too low!啟動溫控系統(tǒng),將溫度升至25Exception : divide by zero!Enter a non-zero denominator again : 4The quotient is: 1*/當以上程序中出現(xiàn)異常時,任何一個捕獲塊都調
53、用基類的 action( ) ,而此action( )函數(shù)能夠根據(jù)不同對象指針來調用相應的虛函數(shù)handling( ), 實現(xiàn)第五章中所述“多態(tài)性”中的“派生類成員函數(shù)的定向調用” 。另外,同一個派生類同時用于處理溫度過高或過低,并區(qū)別地采取相應措施。上面程序中,捕獲程序塊的順序不能顛倒為:catch ( except ex )ex.action( ); catch ( except_derive ex ) ex.action( ); 這時編譯系統(tǒng)將給出警告為:warning: 'class except_derive' is caught by base class (
54、9;class except')這樣一來,其運行結果將不正確,成為:(2)Measured temperature : 40Enter numerator and denominator : 4 8Exception : Temperature too high!Enter a non-zero denominator again : 4Exception : Temperature too high!Enter a non-zero denominator again : 2Exception : Temperature too high!Enter a non-zero denom
55、inator again :這是因為第一個捕獲程序塊也能捕獲異常派生類的對象,從而導致出錯。為何第一個捕獲程序塊也能捕獲異常派生類的對象,但卻又給出錯誤運行結果?見下例: 例 2 形參為基類對象的函數(shù)所使用實參為派生類對象時的錯誤/ obj_inh_1.cpp/ To show object of base class can't be inherited#include <iostream.h>class Pointdouble x, y;public:Point (double i, double j) x=i; y=j; virtual double Area( )
56、const return 0; ;class Rectangle : public Pointdouble w, h;public:Rectangle (double i, double j, double k, double l):Point (i, j) w=k; h=l; double Area( ) const return w*h; ;void fun(Point s)cout<<s.Area( )<<endl;void main()Point p (5, 6);Rectangle rec (3.5, 15.2, 5, 28);cout<<&quo
57、t;Area of Point is :"fun (p);cout<<"Area of Rectangle is :"fun (rec);/* Result:Area of Point is : 0Area of Rectangle is : 0 (當函數(shù)形參是對象而非對象指針時,無法調用派生類部 分的虛函數(shù))*/程序中fun()的形參是基類class Poin田勺對象,但它也能使用派生類對象作為 實參,即fun (rec)。但因其形參已定義為基類對象,因此函數(shù)體內的s.Area()只能調用基類的Area()函數(shù),得出錯誤的運行結果。此處 fun (r
58、ec)是合法的但卻錯 誤的。因止匕,在exception_derived_loop_2.cppl序中,如錯誤地將catch ( except ex ) ex.action( ); 放置于前,則它既會捕獲異常基類又會捕獲異常派生類的對象。而在捕獲異常派 生類對象時,將導致錯誤的運行結果。因此它也是是合法的但卻錯誤的。7.3.3 異常類基類指針和引用的繼承以上 *.3.2"用戶自定義異常類”例1的exception_derived_loop_2.cp沸序 中,為了捕獲兩個不同類(基類和派生類)的對象,使用兩1caich程7塊,即:catch ( except_derive ex ) ex
59、.action( ); catch ( except ex ) ex.action( ); 這兩個程序塊的形參都是異常類的對象。能否合并為一個,例如catch()程序塊?可以!但此合并后的程序塊無法捕獲對象名稱,也就無法區(qū)別并調用相應類 的對象的虛函數(shù)handling(),因這涉及基類對象的繼承問題。第五章中曾提到, 函數(shù)的“按數(shù)值調用”中,派生類對象無法繼承基類對象,但卻能繼承基類對象 的指針和引用。因此須求助于基類對象指針或引用。所以為了合并這兩個 catch 程序塊,必須使用異常類基類的指針或引用而不是基類對象本身。如下:例1異常類基類指針的繼承/ exception_derived_l
60、oop_3.cpp/ user-defined class except and its derived classes/ 和 exception_derived_loop_2.cppf 同之處用下戈U線標出 #include <iostream.h>#define MAX_T 28#define MIN_T 16 int temperature; double denominator; class exceptchar *message;public:except (char * ptr) message = ptr; const char * what( ) const return message; virtual void handling ( ) cout << "Enter a non-zero denominator again :"cin >> denominator; void action ( ) cout << "Exception : " << what( ) << 'n
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度城鎮(zhèn)土地使用權轉讓及配套設施建設合同協(xié)議3篇
- 二零二五年度小額貸款個人信用借款合同范本2篇
- 二零二五年度電子商務銷售結算合同3篇
- 二零二五年度建筑施工安全環(huán)保事故處理協(xié)議3篇
- 二零二五年度個人住宅買賣合同示范范本
- 酒店管理工作中的風險管控
- 醫(yī)院行業(yè)美工的醫(yī)療廣告
- 培訓行業(yè)課程安全操作指南
- 電子工程師的領域探索
- 二零二五年度農產品直銷銷售合同范本
- 醫(yī)院定崗定編方案文檔
- 4-熔化焊與熱切割作業(yè)基礎知識(一)
- 單元教學評一體化設計的探索與實踐以統(tǒng)編語文教材四年級下冊第一單元為例
- 個人安全與社會責任的基本知識概述
- 醫(yī)院標識牌方案設計2
- 移動商務內容運營(吳洪貴)任務二 有效傳播模式的設計
- 簡易勞務合同電子版
- 明代文學緒論
- 體育賽事的策劃、組織與實施 體育賽事利益相關者
- 三級醫(yī)院評審標準(2023年版)實施細則
- 分析化學(高職)PPT完整版全套教學課件
評論
0/150
提交評論