码迷,mamicode.com
首页 > 其他好文 > 详细

Cpp-logic_error vs. runtime_error

时间:2015-08-05 12:41:43      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/wuqi/p/4704312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!