Cpp-logic_error vs. runtime_error

來源:互聯網
上載者:User

標籤:

在stackoverflow上有關於這個的大討論

https://stackoverflow.com/questions/2924058/confused-about-stdruntime-error-vs-stdlogic-error/6476858#6476858?newreg=7f576920c83943698339b83d8ca1f1bd

 

C++0x Standard says (clause 19.2):上是這麼描述的

1) In the error model reflected in these classes (i.e. the exception types), errors are divided into two broad categories: logic errors and runtime errors.

2) The distinguishing characteristic of logic errors is that they are due to errors in the internal logic of the program. In theory, they are preventable.

邏輯錯誤

3) By contrast, runtime errors are due to events beyond the scope of the program. They cannot be easily predicted in advance.

程式的作用範圍造成的( the "scope" is defined as the library code‘s expectations, not your program‘s design)

 

在Cpp標準上也有這樣的描述:

logic_error : "The class logic_error defines the type of objects thrown as exceptions to report errors presumably detectable before the program executes, such as violations of logical preconditions or class invariants." 

在程式執行前,去對一個邏輯上的先決條件進行判斷

runtime_error : "The class runtime_error defines the type of objects thrown as exceptions to report errors presumably detectable only when the program executes." 

在程式執行的過程中,報出錯誤。

 

比如,runtime_error的overflow_error是在程式進行計算(運行)的時候發現的。

而,logic_error的out_of_range,以vector為例,雖然也是程式執行過程中發現的,但是並沒有去取vector中真實資料,而是先檢查了這個“取操作”是否可行。

 

Cpp-logic_error vs. runtime_error

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.