Thinking In C++筆記(一)

來源:互聯網
上載者:User
 

If you don't know what exceptions might occur, don't use exception specification.

Exception specification are mainly for non-template classes. Due to you don't know what will occur when template class constructor throw an exception, you program would terminate.

It is better to seperate two actions into tow seperate member function, which is exactly what the standard stack class does.     (Cohesion : every function should do one thing well!)

Operator = should adhere to the following pattern:

1.  Make sure you’re not assigning to self. If you are, go to step 6. (This is strictly an optimization.)

2.  Allocate new memory required by pointer data members.

3.  Copy data from the old memory to the new.

4.  Delete the old memory.

5.  Update the object’s state by assigning the new heap pointers to the pointer data members.

6.  Return  *this .

 A good technique is to move steps 2 and 3 into a separate function, often called clone()

Don't let destructors throw exceptions

The best advice for deciding when to use exceptions is to throw exceptions only when a function fails to meet its specification.

Don't throw exceptions from interrupt handlers.

使用異常勸誡:

1、 不要在非同步機制中throw exception,尤其是中斷時

2、 異常不是建立機器層級,不會處理除0終端,異常是獨立於程式幾倍的

3、 不要把異常作為switch等常規語句使用,效率太慢了,並且容易導致混亂

4、 如果程式簡單,申請記憶體失敗,讀寫錯誤,不要拋出異常,給出訊息退出即可,不要抓住所有異常,基礎地,不在必要時,不要使用異常。

5、 為什麼C++標準庫中沒有異常規則說明,在舊代碼中編寫新的異常,最好把異常產生代碼(try)獨立出來,並編寫一個異常到已有的錯誤訊息轉換handler

使用異常:

1、 確定一個問題,重複引起異常功能。

2、 修補東西,沒有繼續實驗函數

3、 在當前上下文做任何事,拋出同一個異常到更進階上下文

4、 在當前上下文做任何事,拋出不同的異常到更進階上下文

5、 終止程式

6、 封裝使用普通錯誤機制的函數(尤其是C庫函數),使用異常代替

7、 如果錯誤處理機制使得事情更複雜,異常可以是錯誤處理更簡單和有效

8、 使庫和程式更安全,短期的debugging和長期的robustness

使用exception specification

  Exception specification類似函數原型,但拋出一個unexpected exception時,調用unexpected()函數,因此最好自己寫一個unexpected函數,建立日誌訊息,然後再拋出異常或終止程式。

1、 儘可能採用標準庫中的異常,如果沒有,從標準庫中派生,這樣其他人可以調用exception類中what函數

2、 如果建立自己的異常類,最好把類嵌入到類中或名字空間中,產生一個訊息,避免汙染全域名字空間。

3、 使用類繼承的異常,這樣在自己的異常處理中只需捕獲基礎類類型就可以處理異常。

4、 多重繼承

Catch引用,而不是值,避免:

 1、避免不必要異常對象拷貝

 2、避免對象slicing,異常類對象派生。

建構函式無傳回值

1、 設定一個非局部標識,使用者檢測處理

2、 返回一個不完全建立的對象,使用者檢測。

在構造失敗後繼續執行類對象是非常危險的,因此建構函式是拋出異常的其中一個重要位置。需要注意的是構造拋出異常時清理資源。

如果需要在解構函式中進行異常處理,必須要在解構函式中包含try塊並且在解構函式中處理所有的異常。

異常是運行時的開銷,由於異常出現是rarely,因此,對於這些開銷是值的,with no impacton on execution speed. 

 

Activation record instance (ARI) ------- stack frame -------

A typical stack frame contains the address of the calling function (so execution can return to it)., a pointer to the ARI of the function's static parent.

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.