• <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 鐘謝偉 閱讀(194) 評論(0)  編輯 收藏 引用

            <2012年7月>
            24252627282930
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            導航

            統計

            常用鏈接

            留言簿(1)

            隨筆檔案

            IT網站

            My Friends

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久精品无码一区二区三区| 久久人人爽人人爽人人片AV麻豆| 久久综合色区| 一级女性全黄久久生活片免费 | 久久人人爽人人爽人人片AV东京热 | 久久99精品久久久久子伦| 色综合久久久久网| 色综合久久无码五十路人妻| 精品久久久久久无码免费| 久久影院综合精品| 日本五月天婷久久网站| 99久久亚洲综合精品成人| 欧美国产精品久久高清| 新狼窝色AV性久久久久久| 久久久WWW成人免费毛片| 久久久久免费视频| 国产V综合V亚洲欧美久久| 久久亚洲国产成人精品无码区| 国产精品无码久久综合| 亚洲国产小视频精品久久久三级| 99久久这里只有精品| 国产精品视频久久久| 亚洲午夜久久久影院| 久久AV高清无码| 午夜精品久久久久久中宇| 久久只有这精品99| 久久免费看黄a级毛片| 7777久久久国产精品消防器材| 久久影视综合亚洲| 99久久免费国产精品| 久久精品国产91久久麻豆自制| 久久亚洲精精品中文字幕| 99久久夜色精品国产网站| 久久成人国产精品免费软件| 久久久久久精品免费看SSS| 亚洲欧美国产日韩综合久久| 思思久久99热免费精品6| 久久亚洲国产精品成人AV秋霞| 日韩va亚洲va欧美va久久| 久久人人爽人爽人人爽av | 久久www免费人成精品香蕉|