• <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函數--看VC6.0對Standard C++的支持

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

            #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注冊的函數,默認為terminate
            }

            上面的函數的異常規范指定了只用拋出string型異常,Standard C++會保證該函數只能拋出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確保不發生任何異常
            {
            ?cout<<"Fun is called!"<<endl;
            ?throw string("Excepton is thrown in Fun()");?? //但是throw出了異常,這會調用set_unexpected注冊的函數,默認為terminate
            }
            void Show()
            {
            ??? cout<<"unexpected excepton occurred in Fun()"<<endl;
            ??? system("PAUSE");
            }
            int main()
            {
            ?set_unexpected(Show);?? //VC6并不支持,對Standard C++的支持并不好
            ?try
            ?{
            ??Fun();?? //出現異常,雖有Throw list但仍被throw
            ?}
            ?catch(string str)
            ?{
            ??cout<<"Exception catched!"<<endl<<"description:"<<str<<endl;?
            ?}
            ?
            ?system("PAUSE");
            ?return 0;
            }

            ?? VC6好像并不理會Standard C++的規定。這在Microsoft的網頁上得到了見證:
            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的調用更有優勢.
            ?
            但標準就是標準,不能因為Microsoft不支持標準而標準就會改變。還是有很多Compiler的廠商實現標準。例如我手上的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確保不發生任何異常
            {
            ?cout<<"Fun is called!"<<endl;
            ?throw string("Excepton is thrown in Fun()!");?? //但是throw出了異常,這會調用set_unexpected注冊的函數,默認為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 旅途 閱讀(515) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

            久久精品国产亚洲AV久| 国产精品无码久久综合网| 久久免费精品视频| 色综合久久久久网| 久久综合视频网站| 亚洲精品白浆高清久久久久久 | 久久综合色之久久综合| 亚洲国产精品无码久久九九| 精品国产乱码久久久久软件 | 无码久久精品国产亚洲Av影片| 久久精品国产清高在天天线| 精品无码久久久久久久久久| 伊人色综合久久天天人手人婷| 韩国三级中文字幕hd久久精品| 久久99精品国产麻豆宅宅| 91久久福利国产成人精品| 亚洲精品乱码久久久久久| 久久人人爽人人爽AV片| 久久最近最新中文字幕大全| 乱亲女H秽乱长久久久| 精品久久久一二三区| 久久午夜电影网| 久久精品www| 久久精品国产99国产精偷| 色婷婷综合久久久久中文一区二区| 欧洲国产伦久久久久久久| 国产精品丝袜久久久久久不卡| 久久r热这里有精品视频| 青草国产精品久久久久久| 亚洲AV日韩精品久久久久久久| 久久中文字幕精品| 久久久高清免费视频| 久久久亚洲AV波多野结衣 | 久久香综合精品久久伊人| 久久久噜噜噜久久中文字幕色伊伊 | 国内精品久久久久影院老司| 久久久久亚洲av成人无码电影| 久久综合九色综合久99| 国产精品99久久久久久董美香| 香蕉久久夜色精品国产小说| 久久夜色精品国产亚洲|