日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++异常处理类与自定义异常处理类

發布時間:2024/8/23 c/c++ 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++异常处理类与自定义异常处理类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉自:http://blog.csdn.net/makenothing/article/details/43273137


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

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

[cpp]?view plaincopy print?
  • /*++?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類型的異常。代碼如下:

    [cpp]?view plaincopy print?
  • /*++?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函數,不同的是,也可以手工觸發它的執行。本例的情況屬于前者。代碼如下:

    [cpp]?view plaincopy print?
  • /*++?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的代碼如下:(##為修改之處)

    [cpp]?view plaincopy print?
  • /*++?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都十分近似。修改代碼為:

    [cpp]?view plaincopy print?
  • /*++?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對它進行了說明。

    總結

    以上是生活随笔為你收集整理的C++异常处理类与自定义异常处理类的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。