困惑的std :: runtime_error主场迎战的std :: logic_error [英] Confused about std::runtime_error vs. std::logic_error

查看:279
本文介绍了困惑的std :: runtime_error主场迎战的std :: logic_error的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近看到的升压program_options库抛出一个 logic_error 如果命令行输入是未解析的。这挑战了我的假设约 logic_error runtime_error

I recently saw that the boost program_options library throws a logic_error if the command-line input was un-parsable. That challenged my assumptions about logic_error vs. runtime_error.

我认为逻辑错误( logic_error 及其派生类)均起因于内部故障要坚持程序不变的问题,往往在非法参数内部形式蜜蜂。在这个意义上说,他们在很大程度上是等同主张的,但为了在发布code使用(不像ASSERT的这通常不编译到发布的code)。它们是非常有用的地方是不可行的整合单独的软件在调试/测试部件生成或出现故障的后果是,使得它得到关于该无效不变条件向用户运行时反馈是重要的。

I assumed that logic errors (logic_error and its derived classes) were problems that resulted from internal failures to adhere to program invariants, often in the form of illegal arguments to internal API's. In that sense they are largely equivalent to ASSERT's, but meant to be used in released code (unlike ASSERT's which are not usually compiled into released code.) They are useful in situations where it is infeasible to integrate separate software components in debug/test builds or the consequences of a failure are such that it is important to give runtime feedback about the invalid invariant condition to the user.

同样,我认为 runtime_error 期从运行情况完全导致程序员的控制范围之外:I / O错误,无效的用户输入,等等。

Similarly, I thought that runtime_errors resulted exclusively from runtime conditions outside of the control of the programmer: I/O errors, invalid user input, etc.

不过,program_options显然是严重(主要是?)作为分析最终用户的输入,所以在我的心理模型它当然应该在抛出一个 runtime_error 的手段情况不好的投入。

However, program_options is obviously heavily (primarily?) used as a means of parsing end-user input, so under my mental model it certainly should throw a runtime_error in the case of bad input.

我在哪里去了?你是否异常打字升压模式同意吗?

Where am I going wrong? Do you agree with the boost model of exception typing?

推荐答案

在这种情况下,我认为(至少大部分)你说的没错,这是错误的。该标准描述了 logic_error 为:

In this case, I think (at least for the most part) you're right and it's wrong. The standard describes logic_error as:

类logic_error定义抛出的异常报告错误presumably检测程序执行之前,如违反逻辑preconditions或类不变量的对象的类型。

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.

这是不能被解析的命令行参数似乎并不适合这很好。

A command line argument that can't be parsed doesn't seem to fit that very well.

相反,它描述了 runtime_error 为:

类runtime_error定义的对象抛出的异常报告错误presumably检测程序执行,只有当类型。

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

这似乎是更好的选择。

这篇关于困惑的std :: runtime_error主场迎战的std :: logic_error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆