• <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>

            旅途

            如果想飛得高,就該把地平線忘掉

            set_unexpected函數(shù)--看VC6.0對Standard C++的支持

            ?? 異常規(guī)范(Exception Specification),是通過異常列表(throw list)來規(guī)定一個函數(shù)只能拋出哪些異常。例如:

            #include <iostream>
            #include <string>
            using namespace std;
            void Fun()throw(string)?? //Exception specification,即Throw list確保只拋出string型異常
            {
            ?cout<<"Fun is called!"<<endl;
            ?throw string("Exception occurred!");?? //Ok,會拋出string型異常
            ?//throw int(123);?? //若throw出了int型異常,這會調用set_unexpected注冊的函數(shù),默認為terminate
            }

            上面的函數(shù)的異常規(guī)范指定了只用拋出string型異常,Standard C++會保證該函數(shù)只能拋出string型異常。若拋出其它異常都是不期望的(Unexpected)。
            ???但這對Microsoft VC++6.0卻不成立。試寫以下代碼:

            // Platform: WinXp + VC6.0
            #include <stdlib.h>
            #include <iostream>
            #include <string>
            using namespace std;
            void Fun()throw()?? //Exception specification,即Throw list確保不發(fā)生任何異常
            {
            ?cout<<"Fun is called!"<<endl;
            ?throw string("Excepton is thrown in Fun()");?? //但是throw出了異常,這會調用set_unexpected注冊的函數(shù),默認為terminate
            }
            void Show()
            {
            ??? cout<<"unexpected excepton occurred in Fun()"<<endl;
            ??? system("PAUSE");
            }
            int main()
            {
            ?set_unexpected(Show);?? //VC6并不支持,對Standard C++的支持并不好
            ?try
            ?{
            ??Fun();?? //出現(xiàn)異常,雖有Throw list但仍被throw
            ?}
            ?catch(string str)
            ?{
            ??cout<<"Exception catched!"<<endl<<"description:"<<str<<endl;?
            ?}
            ?
            ?system("PAUSE");
            ?return 0;
            }

            ?? VC6好像并不理會Standard C++的規(guī)定。這在Microsoft的網(wǎng)頁上得到了見證:
            In the current Microsoft implementation of C++ exception handling, unexpected calls terminate by default and is never called by the exception-handling run-time library. There is no particular advantage to calling unexpected rather than terminate.
            ???Microsoft并不覺得unepxpected的處理會比terminate的調用更有優(yōu)勢.
            ?
            但標準就是標準,不能因為Microsoft不支持標準而標準就會改變。還是有很多Compiler的廠商實現(xiàn)標準。例如我手上的Dev-C++5.0 beta版。(華軍軟件園上有下載)就對標準支持較好。

            //Platform: WinXp + Dev-C++5.0 beta
            #include <stdlib.h>
            #include <iostream>
            #include <string>
            using namespace std;
            void Fun()throw()?? //Exception specification,即Throw list確保不發(fā)生任何異常
            {
            ?cout<<"Fun is called!"<<endl;
            ?throw string("Excepton is thrown in Fun()!");?? //但是throw出了異常,這會調用set_unexpected注冊的函數(shù),默認為terminate
            }
            void Show()
            {
            ??? cout<<"unexpected excepton occurred!"<<endl;
            ??? system("PAUSE");
            }
            int main()
            {
            ?set_unexpected(Show);?? //Dec-C++支持,對Standard C++的支持較好
            ?try
            ?{
            ??Fun();???? //Show() is invoked when Fun() throw the exception
            ?????????????//此時,代碼跳至Show中處理
            ?}
            ?catch(...)
            ?{
            ??cout<<"Catch exception!"<<endl;
            ?}
            ?
            ?system("PAUSE");
            ?return 0;
            ?
            }

            ?
            ?? 其實上面也只是說明VC6對標準C++支持并不是十分完全,但這并不是什么壞事。相反VC6還是很好用的。不過對Standard C++的有些特性不支持,學習C++語言時可就要留個心眼了。當你看《C++ Primer》等標準C++的教材時,如果書有些例子拿到VC6中并不能編譯通過,再懷疑代碼敲錯或書上代碼印錯的同時還得想想是不是VC6不夠 Standard.(不過還好的是VC6.0基本上%90以上還是挺標準的)。
            ?
            ?
            ?
            ?
            ?
            ?
            ?
            ?
            ?
            ?
            Jerry.Chow
            ?????? 9/18'07

            http://blog.chinaunix.net/u/11680/showart.php?id=384295

            posted on 2007-09-29 00:55 旅途 閱讀(511) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

            国内精品久久国产| 久久久久夜夜夜精品国产| 久久综合偷偷噜噜噜色| 精品国产乱码久久久久久人妻 | 欧美噜噜久久久XXX| 国产精品久久久久久一区二区三区| 四虎国产永久免费久久| 亚洲国产日韩欧美久久| 7777久久久国产精品消防器材| 久久777国产线看观看精品| 亚洲国产成人久久综合一区77 | 亚洲综合伊人久久综合| 激情五月综合综合久久69| 亚洲乱码精品久久久久..| 久久艹国产| 国内精品伊人久久久久av一坑| 久久婷婷五月综合97色直播| 国产情侣久久久久aⅴ免费| 久久亚洲精品国产亚洲老地址| 国产成人精品久久| 久久成人国产精品二三区| 亚洲精品国产美女久久久| 午夜视频久久久久一区| 91精品观看91久久久久久| 久久精品国产亚洲AV嫖农村妇女| 思思久久99热免费精品6| 久久影院久久香蕉国产线看观看| 久久丝袜精品中文字幕| 99久久综合国产精品二区| 久久99精品综合国产首页| 久久精品aⅴ无码中文字字幕不卡| 超级97碰碰碰碰久久久久最新| 香蕉久久永久视频| 亚洲欧洲久久av| 久久影视综合亚洲| 亚洲国产精品一区二区三区久久| 日本欧美国产精品第一页久久| 久久夜色撩人精品国产| 99久久综合国产精品免费| 亚洲中文字幕久久精品无码APP| 人妻无码久久一区二区三区免费|