• <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
            <2007年6月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            My Tech blog

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

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

            #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:拋出不可控異常,執(zhí)行unexpected函數(shù)
                    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;
                }
            }
            上面的程序中,執(zhí)行1,會產(chǎn)生如下的結(jié)果:
            E1 error occured
            執(zhí)行2,并沒有被作為類型E2的異常所捕獲.而是出現(xiàn)了如下提示:
            terminate called after throwing an instance of 'E2'
            忽略
            現(xiàn)在執(zhí)行3.結(jié)果并沒有捕獲到std::bad_exception異常.而是仍然出現(xiàn)了這樣的錯誤提示:
            terminate called after throwing an instance of 'E2'
            忽略
            同樣的的例子,上面是在linux下執(zhí)行的情況.下面換做vs2003的vc7.執(zhí)行3,結(jié)果是:
            unknown exception occured
            即也沒有捕獲到std::bad_exception,但是被更加通用的...所捕獲了.這是編譯器情況的不同.
            2.c++標準庫的異常(Exceptions for the C++ standard library)
            c++標準庫的異常類通常從logic_error類繼承.邏輯錯誤從理論上來說,至少應該被程序所避免,例如通過對于函數(shù)參數(shù)進行額外的測試.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>中.
            異常類的成員
            在異常類中除類型信息之外,唯一可以獲取的信息來自于函數(shù)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;
                ...
            }
            可以以下面的方式創(chuàng)建標準異常:
            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 閱讀(1045) 評論(0)  編輯 收藏 引用 所屬分類: 我的讀書筆記
            欧洲人妻丰满av无码久久不卡| 亚洲国产二区三区久久| 亚洲精品乱码久久久久久蜜桃| 2020国产成人久久精品| 久久久久久久97| 国产亚州精品女人久久久久久 | 伊人久久无码精品中文字幕| 久久久久久精品免费看SSS| 青青草国产成人久久91网| 久久国产亚洲精品| 999久久久国产精品| 久久夜色精品国产噜噜亚洲AV | 精品久久久一二三区| a级成人毛片久久| 久久久www免费人成精品| 伊人久久综在合线亚洲2019 | 色8久久人人97超碰香蕉987| 久久久久无码精品国产app| 99久久人妻无码精品系列蜜桃| 中文字幕久久亚洲一区| 久久久久99精品成人片三人毛片| 77777亚洲午夜久久多喷| 久久99久国产麻精品66| 无码任你躁久久久久久久| 久久se精品一区二区影院| 77777亚洲午夜久久多喷| 日韩AV无码久久一区二区| 狠狠色婷婷久久综合频道日韩 | 2021久久精品免费观看| 久久久中文字幕日本| 国产精品美女久久久久av爽 | 欧美激情精品久久久久| 久久久久久人妻无码| 国产成人久久精品一区二区三区| 精品久久久久久久国产潘金莲| 久久婷婷是五月综合色狠狠| 久久久久久久综合狠狠综合| 伊人久久大香线蕉亚洲五月天| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 伊人久久亚洲综合影院| 久久久久久久女国产乱让韩|