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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ throw:抛出自己的异常

發(fā)布時間:2023/12/4 c/c++ 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ throw:抛出自己的异常 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
throw 是C++中的關(guān)鍵字,用來拋出異常。如果不使用 throw 關(guān)鍵字,try 就什么也捕獲不到;上節(jié)提到的 at() 函數(shù)在內(nèi)部也使用了 throw 關(guān)鍵字來拋出異常。

throw 既可以用在標(biāo)準(zhǔn)庫中,也可以用在自定義的函數(shù)中,拋出我們期望的異常。throw 關(guān)鍵字語法為:

throw exceptionData;

exceptionData 是“異常數(shù)據(jù)”的意思,它既可以是一個普通變量,也可以是一個對象,只要能在 catch 中匹配就可以。

下面的例子演示了如何使用 throw 關(guān)鍵字:
  • #include <iostream>
  • #include <string>
  • using namespace std;
  • char get_char(const string &, int);
  • int main(){
  • string str = "c plus plus";
  • try{
  • cout<<get_char(str, 2)<<endl;
  • cout<<get_char(str, 100)<<endl;
  • }catch(int e){
  • if(e==1){
  • cout<<"Index underflow!"<<endl;
  • }else if(e==2){
  • cout<<"Index overflow!"<<endl;
  • }
  • }
  • return 0;
  • }
  • char get_char(const string &str, int index){
  • int len = str.length();
  • if(index < 0)
  • throw 1;
  • if(index >= len)
  • throw 2;
  • return str[index];
  • }
  • 運行結(jié)果:
    p
    Index overflow!

    在 get_char() 函數(shù)中,我們使用了 throw 關(guān)鍵字,如果下標(biāo)越界,就會拋出一個 int 類型的異常:如果是下溢,異常數(shù)據(jù)的值為 1;如果是上溢,異常數(shù)據(jù)的值為 2。在 catch 中,將捕獲 int 類型的異常,然后根據(jù)異常數(shù)據(jù)輸出不同的提示語。

    不被建議的用法

    throw 關(guān)鍵字除了可以用在函數(shù)體中拋出異常,還可以用在函數(shù)頭和函數(shù)體之間,指明函數(shù)能夠拋出的異常類型。有些文檔中稱為異常列表。例如: double func (char param) throw (int); 這條語句聲明了一個名為 func 的函數(shù),它的返回值類型為 double,有一個 char 類型的參數(shù),并且只能拋出 int 類型的異常。如果拋出其他類型的異常,try 將無法捕獲,只能終止程序。

    如果希望能夠拋出多種類型的異常,可以用逗號隔開: double func (char param) throw (int, char, exception); 如果不希望限制異常類型,那么可以省略: double func (char param) throw (); 如此,func() 函數(shù)可以拋出任何類型的異常,try 都能捕獲到。

    更改上例中的代碼:
  • #include <iostream>
  • #include <string>
  • using namespace std;
  • char get_char(const string &, int) throw(char, exception);
  • int main(){
  • string str = "c plus plus";
  • try{
  • cout<<get_char(str, 2)<<endl;
  • cout<<get_char(str, 100)<<endl;
  • }catch(int e){
  • if(e==1){
  • cout<<"Index underflow!"<<endl;
  • }else if(e==2){
  • cout<<"Index overflow!"<<endl;
  • }
  • }
  • return 0;
  • }
  • char get_char(const string &str, int index) throw(char, exception){
  • int len = str.length();
  • if(index < 0)
  • throw 1;
  • if(index >= len)
  • throw 2;
  • return str[index];
  • }
  • 在使用 GCC 的 IDE 中運行代碼,執(zhí)行到第 12 行時程序會崩潰。雖然 func 函數(shù)檢測到下標(biāo)越界,知道發(fā)生了異常,但是由于 throw 限制了函數(shù)只能拋出 char、exception 類型的異常,所以 try 將捕獲不到異常,只能交給系統(tǒng)處理,終止程序。

    需要說明的是,C++標(biāo)準(zhǔn)已經(jīng)不建議這樣使用 throw 關(guān)鍵字了,因為各個編譯器對 throw 的支持不同,有的直接忽略,不接受 throw 的限制,有的將 throw 作為函數(shù)簽名,導(dǎo)致引用函數(shù)時可能會有問題。上面的代碼在 GCC 下運行時會崩潰,在 VS 下運行時則直接忽略 throw 關(guān)鍵字對異常類型的限制,try 可以正常捕獲到 get_char() 拋出的異常,程序并不會崩潰。

    總結(jié)

    以上是生活随笔為你收集整理的C++ throw:抛出自己的异常的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。