2021年6月11日 星期五

讀書心得 - C++ Primer (5th Edition) - Chapter 5 (2) - Exception

Exceptions

Exceptions 是程式在 run-time 遇到的異常,比如與資料庫的連結斷開或遇到其他程式異想不到的 input。Exception handling 基本上就是當程式無法解決問題或者無法繼續執行時發出警告,或者更進一步根據情況處理 exception。
Exception handling 由 detecting, handling parts of a program 所組成
  • throw expressions
    當 detecting part 表示遭遇無法處理的情況
  • try blocks
    handling part 用來處理 exception。程式碼一開始會有一個 try block 隨後跟著一個或以上的 catch clauses。
  • exception classes
    用來傳遞 information about what happened between a throw and an associated catch.

A throw Expression

通常會在 detecting part 發生 exception 時 throw Expression,這麼做原因是有時你不能保證你的程式一定是跟 User 互動的那一個,throw Expression 效果就比你回傳一個錯誤值好用得多。
    // Normal version
    int throwTest(int num1, int num2) {
        if (num1 == num2) {
            cout << "Same." << endl;
            return 0;
        } else {
            cerr << "Not the same." << endl;
            return -1;
        }
    }

    // throw Expression
    void throwTest(int num1, int num2) {
        if (num1 != num2)
            throw runtime_error("Not the same.");
        cout << "Same." << endl;
    }

The try Block

基於 C 風格的錯誤處理,在回傳值代表發生錯誤時予以處理。相比之下 throw 就相對靈活,但也會有終止程式的代價。try catch 的 block 就適合在此情況使用。讓程式有 exception handler。
    try {
        program-statements
    } catch (exception-declaration) {
        handler-statements
    } catch (exception-declaration) {
        handler-statements
    } // . . .

Standard Exceptions

正如上面的 try block 示億程式碼所示,程式必須先宣告 exception 的種類才能近一步去做 handle,這裡紀錄 C++ standard library \ 裡的 exception table。 
    exception         // The most general kind of problem.

    // Runtime errors
    runtime_error     // Problem that can be detected at the run-time.
    range_error       // Result generated outside the range of value that are meaningful.
    overflow_error    // Computation that overflow.
    underflow_error   // Computation that underflow.

    // Logic errors
    logic_error       // Error in the logic of the program.
    domain_error      // Argument for which no result exists.
    invalid_argument  // Inappropriate argument.
    length_error      // Attempt to create an object larger than the maximum size for that type.
    out_of_range      // Use a value outside the valid range.

Related Posts:

  • 讀書心得 - 大腦衝浪 - 第十三講第十三講 尤利西斯合約 尤利西斯在打贏特洛伊戰爭後,在返鄉的路上發現自己會經過 Siren 海妖的地盤,傳說海妖幽美的歌聲會迷惑水手,並讓其自行觸礁沈船。尤利西斯很想聽看看那歌聲,所以命令水手把他綁在甲板的桅杆上,並讓自己的手下用蠟堵住耳朵,拼命划不要理他的叫喊。 我想簽下未來的強制合約似乎有點用,目前最想改的是起床划手機的惡習。… Read More
  • 讀書心得 - 大腦衝浪 - 第十六講 第十六講 對的時間,做對的事 說實在這很難,我通常晚上狀態最好,所以沉住氣不要回家通常都會很充實。但到了家發現耍廢的時間不夠,影響到睡眠 => 隔天 => 早上 => 晚上又好了... 我想要先拓展對的時間,所以還是制定個早睡早起的習慣… Read More
  • 讀書心得 - 大腦衝浪 - 第二十三講第二十三講 障礙預報 我想睡眠是最大的障礙預報,曾設過鬧鐘規定時間去睡覺但一點效都沒,我的潛意識裡認為晚上10點還早,但其實過了 10 才在做一天收尾已經算晚了,所以設定晚上 9:30。 但懶惰的狀況總是發生,我想要折衷,出發是必須即使只有 1 %的 CP 值,因為 1 > 0,1 還可以累加。… Read More
  • 讀書心得 - 大腦衝浪 - 第二十一講第二十一講 正向幻想 我想應該是像叔叔一樣的生活吧,靠著投資為主本業為輔的生活,我想當個打打程式且不被薪水跟進度摧殘的程式設計師,百分之百的享受寫程式帶來的成就感而不因是主要收入來源而討厭。 想著清晨  6:30 起床,吃著早餐看本書到 7:30,運動到 9:00 並回家洗澡,10:00上班 ~ 17:00 下班吃飯,參加著某個社團活動到 21:00 回家,23:00 就寢。 六日在家的話,同樣行程到 10:00 便睡到中午吃中… Read More
  • 讀書心得 - 大腦衝浪 - 第十一講第十一講 騰出生活的空間 我想我有一狗票東西得丟了,舊的不去新的不來。 留東西前先問自己三遍,愛它嗎 ? 能幹嘛 ? 確定嗎 ? 目標 : 一輩不會再打開的書 沒開過的海報 沒穿過的鞋子 掛到發霉的衣服 擺到發霉的褲子 抽屜裡的廢物 當兵的雜物 沒用的箱子 沒用的袋子… Read More

0 意見:

張貼留言

Popular Posts