• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 181  文章 - 15  trackbacks - 0
            <2008年11月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            30123456

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            My Tech blog

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            ;C++標準庫風格迥異.它包含了設計和實現風格各不相同的不同來源的軟件.錯誤處理和異常處理是一個典型.標準庫中的string類們支持更加細化的錯誤處理.它們會監測每一處有可能發生問題的地方并會在有錯誤的時候拋出異常.另外一些,比如STL和valarrays比較安全性而言更加注重效率.所以它們很少監測邏輯錯誤,并僅僅在運行時錯誤發生的時候才拋出異常.
            有三種標準異常類:
            1.語言支持型異常(Exceptions for language support )
            比如:
            用new創建對象失敗的時候,會拋出bad_alloc異常;
            dynamic_cast失敗會拋出bad_cast異常;
            函數拋出不可控異常的時候會拋出bad_exception異常或調用unexpected函數.

            #include <iostream>
            class E1
            {

            };
            class E2
            {
            };
            void f_throwE1() throw(E1)
            {
                
            throw E1();
            }
            void f_throwE2() throw(E1)
            {
                
            throw E2();
            }
            void f_throwE2AsBadException() throw (E1,std::bad_exception)
            {
                
            throw E2();
            }
            int main()
            {
                
            try
                {

                    f_throwE1(); 
            //1:拋出可控異常
                    f_throwE2(); //2:拋出不可控異常,執行unexpected函數
                    f_throwE2AsBadException(); //3:拋出不可控異常,被認為是bad_exception
                }
                
            catch(const E1 &e1)
                {
                    std::cout
            <<"E1 error occured"<<std::endl;    
                }
                
            catch(const std::bad_exception &e2)
                {
                    std::cout
            <<"bad_exception occured"<<std::endl;
                }
                
            catch(const E2 &e2)
                {
                    std::cout<<"E2 error occured"<<std::endl;
                }
                catch(...)
                {
                    std::cout<<"unknown exception occured"<<std::endl;
                }
            }
            上面的程序中,執行1,會產生如下的結果:
            E1 error occured
            執行2,并沒有被作為類型E2的異常所捕獲.而是出現了如下提示:
            terminate called after throwing an instance of 'E2'
            忽略
            現在執行3.結果并沒有捕獲到std::bad_exception異常.而是仍然出現了這樣的錯誤提示:
            terminate called after throwing an instance of 'E2'
            忽略
            同樣的的例子,上面是在linux下執行的情況.下面換做vs2003的vc7.執行3,結果是:
            unknown exception occured
            即也沒有捕獲到std::bad_exception,但是被更加通用的...所捕獲了.這是編譯器情況的不同.
            2.c++標準庫的異常(Exceptions for the C++ standard library)
            c++標準庫的異常類通常從logic_error類繼承.邏輯錯誤從理論上來說,至少應該被程序所避免,例如通過對于函數參數進行額外的測試.c++提供了諸如invalid_argument,length error,out_of_range,domain_error等異常類.
            3.在程序域之外的異常(Exceptions for errors outside the scope of a program)
            此類異常通常難于避免.
            異常類的頭文件
            異常的基類和bad_exception類被定義在<exception>里;
            類bad_alloc在<new>中被聲明;
            bad_cast和bad_typeid在<typeinfo>中被聲明;
            ios_base::failure在<ios>中被聲明;
            所有其他的類都被定義在<stdexcept>中.
            異常類的成員
            在異常類中除類型信息之外,唯一可以獲取的信息來自于函數what():
            namespace std {
                class exception {
                  public:
                        virtual const char* what() const throw();
                        ...
                  };
            }

            使用what來獲取信息:
            try {
                ...
            }
            catch (const exception& error) {
                //print implementation-defined error message
                cerr << error.what() << endl;
                ...
            }
            可以以下面的方式創建標準異常:
            string s;
            ...
            throw out_of_range(s);

            或者
            throw out_of_range("out_of_range exception (somewhere, somehow)");
            從標準異常類繼承
            例如:
            #include <exception>
            #include 
            <iostream>
            class MyException:public std::exception
            {
            private:
                
            char* errorMessage;
            public:
                MyException(
            const char* errMsg)
                {
                    errorMessage
            =new char[strlen(errMsg)];
                    strcpy(errorMessage,errMsg);
                }
                
            ~MyException() throw()
                {
                    
            if(errorMessage)
                    {
                        delete[] errorMessage;
                        errorMessage
            =NULL;
                    }
                }
                
            virtual const char* what() const throw()
                {
                    
            return errorMessage;
                }

            };
            int main()
            {
                
            try
                {
                    
            throw MyException("Error Occured!!");
                }
                
            catch(const std::exception &e)
                {
                    std::cout
            <<e.what()<<std::endl;
                }

            }






            posted on 2007-06-26 22:18 littlegai 閱讀(1055) 評論(0)  編輯 收藏 引用 所屬分類: 我的讀書筆記
            国内精品久久久久久久久电影网 | 少妇久久久久久被弄到高潮| 青青青青久久精品国产h| 精品无码久久久久久国产| 亚洲欧美日韩中文久久| 国产高清国内精品福利99久久| 国产99久久久国产精品小说| 久久久91精品国产一区二区三区 | 国内精品久久久久久久久| 国产69精品久久久久9999APGF| 久久精品国产精品青草app| 久久精品国产亚洲av麻豆蜜芽| 久久精品嫩草影院| 亚洲中文字幕久久精品无码APP| 国产精品gz久久久| 久久超乳爆乳中文字幕| 久久这里只有精品首页| 激情综合色综合久久综合| 91精品国产色综合久久| 香蕉久久夜色精品国产2020| 精品国产91久久久久久久a| 久久久精品人妻一区二区三区蜜桃| 久久久不卡国产精品一区二区| 久久综合狠狠色综合伊人| 久久精品一本到99热免费| 亚洲精品无码久久一线| 久久狠狠爱亚洲综合影院| 久久久国产视频| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 久久精品这里热有精品| 久久精品亚洲一区二区三区浴池| 国产成人精品综合久久久| 97久久婷婷五月综合色d啪蜜芽| 久久91精品国产91| 欧美日韩久久中文字幕| 国内高清久久久久久| 欧洲成人午夜精品无码区久久| 无码精品久久久天天影视| 国产V亚洲V天堂无码久久久| 国内精品久久久久久野外| 99精品国产在热久久|