標籤:
在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