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

            錯誤處理

            編程中一般出現的錯誤有兩種:語法上的錯誤與語義上的錯誤,語法中的錯誤幾乎都能夠被編譯器識別出來,但是語義上的錯誤編譯器就無法識別,因此在編程的過程中應該時刻警惕語義錯誤的出現。

            在編程初始,我們可以事先編寫防錯的程序段,一般錯誤會出現在什么地方呢?

            1)函數調用時傳遞的參數的有效性

            2)函數的返回值可能隱含了錯誤發生的可能

            3)從用戶或文件中讀取的輸入導致的錯誤

            為了很好的避免這些錯誤,我們通常遵循如下的措施:

            1)在函數的頂部,確保傳遞進來的參數有效

            2)函數調用后,檢測返回值

            3)使輸入的值有效,符合規范

            那么選取怎樣的方式對錯誤進行處理呢,一般如下:

            1)快速跳過代碼

               1: void PrintString(char *strString)
               2: {
               3:     // Only print if strString is non-null
               4:     if (strString)
               5:         std::cout << strString;
               6: }

            2)想調用程序中,返回錯誤碼

               1: int g_anArray[10]; // a global array of 10 characters
               2:  
               3: int GetArrayValue(int nIndex)
               4: {
               5:     // use if statement to detect violated assumption
               6:     if (nIndex < 0 || nIndex > 9)
               7:        return -1; // return error code to caller
               8:  
               9:     return g_anArray[nIndex];
              10: }

            3)如果你想在發現錯誤時直接終止程序,可以使用exit函數

               1: int g_anArray[10]; // a global array of 10 characters
               2:  
               3: int GetArrayValue(int nIndex)
               4: {
               5:     // use if statement to detect violated assumption
               6:     if (nIndex < 0 || nIndex > 9)
               7:        exit(2); // terminate program and return error number 2 to OS
               8:  
               9:     return g_anArray[nIndex];
              10: }

            4)如果用戶輸入不合理的值,就繼續要求輸入

               1: char strHello[] = "Hello, world!";
               2:  
               3: int nIndex;
               4: do
               5: {
               6:     std::cout << "Enter an index: ";
               7:     std::cin >> nIndex;
               8: } while (nIndex < 0 || nIndex >= strlen(strHello));
               9:  
              10: std::cout << "Letter #" << nIndex << " is " << strHello[nIndex] << std::endl;

            5)可以使用cerr輸出錯誤,在gui編程的時候,可以直接在遇到錯誤的時候跳出對話框,輸出錯誤碼,然后終止

               1: void PrintString(char *strString)
               2: {
               3:     // Only print if strString is non-null
               4:     if (strString)
               5:         std::cout << strString;
               6:     else
               7:         std::cerr << "PrintString received a null parameter";
               8: }

             

            另,可以使用assert

               1: int g_anArray[10]; // a global array of 10 characters
               2:  
               3: #include <cassert> // for assert()
               4: int GetArrayValue(int nIndex)
               5: {
               6:     // we're asserting that nIndex is between 0 and 9
               7:     assert(nIndex >= 0 && nIndex <= 9); // this is line 7 in Test.cpp
               8:  
               9:     return g_anArray[nIndex];
              10: }

            posted on 2012-05-28 21:05 鐘謝偉 閱讀(192) 評論(0)  編輯 收藏 引用

            <2012年5月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿(1)

            隨筆檔案

            IT網站

            My Friends

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            segui久久国产精品| 久久综合亚洲欧美成人| 久久福利片| 无码日韩人妻精品久久蜜桃| 久久人人爽人人爽AV片| 久久久久亚洲精品天堂| 久久久久久免费视频| 99久久人妻无码精品系列蜜桃| 99久久国产亚洲综合精品| 国产高清国内精品福利99久久| 亚洲国产小视频精品久久久三级 | 精品久久久久久久国产潘金莲| 日韩人妻无码一区二区三区久久99| 久久久精品国产sm调教网站 | 色综合久久久久无码专区| 久久中文娱乐网| 国产成人精品免费久久久久| 久久久久久亚洲精品无码| 久久国产精品波多野结衣AV| 久久久久青草线蕉综合超碰| 中文字幕久久精品| 久久精品国产99国产精偷| 一级做a爰片久久毛片人呢| 久久久久夜夜夜精品国产| 欧美成人免费观看久久| 国产成人无码精品久久久免费| 精品无码久久久久国产| 麻豆av久久av盛宴av| 中文字幕无码久久人妻| 久久亚洲国产精品123区| 久久精品成人国产午夜| 国产精品一区二区久久| 国产精品久久久久久搜索 | 久久九九久精品国产| 99久久精品费精品国产一区二区| 人妻无码αv中文字幕久久琪琪布| 久久久久女教师免费一区| 欧美午夜A∨大片久久| 久久精品中文字幕大胸| 欧美成人免费观看久久| 欧美精品乱码99久久蜜桃|