• <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對(duì)Standard C++的支持

            ?? 異常規(guī)范(Exception Specification),是通過(guò)異常列表(throw list)來(lái)規(guī)定一個(gè)函數(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,會(huì)拋出string型異常
            ?//throw int(123);?? //若throw出了int型異常,這會(huì)調(diào)用set_unexpected注冊(cè)的函數(shù),默認(rèn)為terminate
            }

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

            // 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出了異常,這會(huì)調(diào)用set_unexpected注冊(cè)的函數(shù),默認(rèn)為terminate
            }
            void Show()
            {
            ??? cout<<"unexpected excepton occurred in Fun()"<<endl;
            ??? system("PAUSE");
            }
            int main()
            {
            ?set_unexpected(Show);?? //VC6并不支持,對(duì)Standard C++的支持并不好
            ?try
            ?{
            ??Fun();?? //出現(xiàn)異常,雖有Throw list但仍被throw
            ?}
            ?catch(string str)
            ?{
            ??cout<<"Exception catched!"<<endl<<"description:"<<str<<endl;?
            ?}
            ?
            ?system("PAUSE");
            ?return 0;
            }

            ?? VC6好像并不理會(huì)Standard C++的規(guī)定。這在Microsoft的網(wǎng)頁(yè)上得到了見(jiàn)證:
            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并不覺(jué)得unepxpected的處理會(huì)比terminate的調(diào)用更有優(yōu)勢(shì).
            ?
            但標(biāo)準(zhǔn)就是標(biāo)準(zhǔn),不能因?yàn)镸icrosoft不支持標(biāo)準(zhǔn)而標(biāo)準(zhǔn)就會(huì)改變。還是有很多Compiler的廠商實(shí)現(xiàn)標(biāo)準(zhǔn)。例如我手上的Dev-C++5.0 beta版。(華軍軟件園上有下載)就對(duì)標(biāo)準(zhǔn)支持較好。

            //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出了異常,這會(huì)調(diào)用set_unexpected注冊(cè)的函數(shù),默認(rèn)為terminate
            }
            void Show()
            {
            ??? cout<<"unexpected excepton occurred!"<<endl;
            ??? system("PAUSE");
            }
            int main()
            {
            ?set_unexpected(Show);?? //Dec-C++支持,對(duì)Standard C++的支持較好
            ?try
            ?{
            ??Fun();???? //Show() is invoked when Fun() throw the exception
            ?????????????//此時(shí),代碼跳至Show中處理
            ?}
            ?catch(...)
            ?{
            ??cout<<"Catch exception!"<<endl;
            ?}
            ?
            ?system("PAUSE");
            ?return 0;
            ?
            }

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

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

            posted on 2007-09-29 00:55 旅途 閱讀(515) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): C/C++

            久久国产精品二国产精品| 久久综合九色综合网站| 91精品国产综合久久婷婷| 国产69精品久久久久久人妻精品| 久久99热这里只有精品国产| 久久亚洲精品成人av无码网站 | 久久精品99久久香蕉国产色戒| 久久婷婷国产综合精品| 91精品国产高清久久久久久91| 香蕉久久久久久狠狠色| 国内精品久久久久| 综合久久一区二区三区 | 久久综合成人网| 久久久久亚洲av无码专区| 久久91精品综合国产首页| 伊人久久精品无码av一区 | 亚洲va中文字幕无码久久不卡| 久久久精品午夜免费不卡| 亚洲v国产v天堂a无码久久| 77777亚洲午夜久久多喷| 中文成人久久久久影院免费观看| 久久精品亚洲一区二区三区浴池 | 日本精品久久久久中文字幕| 囯产极品美女高潮无套久久久| 国产999精品久久久久久| 麻豆亚洲AV永久无码精品久久| 亚洲精品乱码久久久久久蜜桃| 国产欧美久久久精品| 亚洲国产一成人久久精品| 亚洲国产成人久久综合区| 国产精品久久久天天影视香蕉| 99久久精品日本一区二区免费| 中文字幕精品久久久久人妻| 久久无码一区二区三区少妇| 久久美女人爽女人爽| 精品久久久久久国产潘金莲| 精品一二三区久久aaa片| 99久久精品免费看国产一区二区三区| 欧美成a人片免费看久久| 欧美伊人久久大香线蕉综合69| 国产精品午夜久久|