C ++错误代码vs ASSERTS VS异常选择选择:( [英] C++ error-codes vs ASSERTS vs Exceptions choices choices :(

查看:251
本文介绍了C ++错误代码vs ASSERTS VS异常选择选择:(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有问题的代码

我听说过(反感)了栅栏两边的C ++异常咒语。这已经有一段时间了,我只是想再次集中自己,这个讨论特定于我链接的代码(或低级类,如容器),它的依赖。我以前是一个防御和error_code使用C程序员,但它是一个令人讨厌的做法,我现在在更高的抽象层次编程。

I have heard (and regurgitated) the C++ exception mantra on both sides of the fence. It has been a while and I just want to centre myself once more, and this discussion is specific to the code I have linked (or low level classes such as containers), and it's dependencies. I used to be a defensive and error_code using C programmer, but it's a tiresome practise and I am programming at a higher level of abstraction now.

所以我重写一个容器类(和它的依赖)更灵活和阅读更好(迭代器缺席atm)。正如你可以看到我回传枚举的error_codes,我知道我会在call-site测试他们。这些容器用于运行时构建AST,初始化和只读。例外是他们防止容器被天真地使用(可能由未来自己使用)。

So I am rewriting a container class (and it's dependencies) to be more flexible and read better (iterators absent atm). As you can see I am returning enumerated error_codes where I know I will test them at call-site. The containers are for runtime building of AST's, initialize and make read-only. The exceptions are their to prevent the container being used naively (possibly by myself in the future).

我在这个课程的地方有例外,他们让我感觉很脏。我欣赏他们的用例。如果我有选择,我可以把他们完全(Boost使用例外很多,我建立Boost,是的,我知道他们可以被禁用,但在罗马....)。我有选择替换他们与error_codes,但嘿,我不会测试他们,所以是什么点?

I have exceptions all over the place in this class, and they make me feel dirty. I appreciate their use-case. If I had the choice I might turn them off altogether (Boost uses exceptions a lot, and I am building off Boost, and yes I know they can be disabled, but when in Rome....) . I have the choice of replacing them with error_codes but hey, I will not test them , so what is the point ?

我应该用ASSERTS替换它们吗?这是什么人膨胀的人说出来 [1] [2] [3] ?每个功能callsite是否获得额外的机械?或只有那些有catch子句?因为我不会抓住这些例外,我不应该成为这种膨胀权的受害者? ASSERTS不会进入发布版本,在基本的原始类(即容器)的上下文中,甚至是重要的吗?我的意思是,逻辑错误会进入最终构建的机会有多高?

Should I replace them with ASSERTS ? What is this bloat people speak off [1] [2] [3]? does every function callsite get extra machinery ? or only those that have a catch clause ? Since I won't catch these exceptions I shouldn't be a victim of this bloatage right ? ASSERTS do not make their way into release builds, in the context of fundamental primitive classes ( -- i.e, containers) does that even matter ? I mean how high are the chances that logic errors would find their way into a final build ?

由于我们想回答重点突出的问题,这里是我的:您会做什么,为什么?:D

Since we like to answer focused questions, here is mine: What would you do, and why ? :D

不相关的连结:

Unrelated Link:Error codes and having them piggy backing in an exception.

编辑2 在此特定的错误代码中出现错误代码,例如:hrel =nofollow case选择是在ASSERT和异常之间,我认为异常最有意义,如上所述,容器只在初始化后读取,大多数异常在初始化时触发。

edit 2 in this particular case the choice is between ASSERTs and exceptions, I think exceptions make the most sense, as I mentioned above, the container is read only after initialisation, and most of the exceptions are triggered during initialisation.

推荐答案

这很简单。避免错误代码,如火和喜欢异常,除非错误代码真的在某种情况下更有意义。为什么?因为异常可以携带更多的信息 - 参见例如。 Boost.Exception。因为它们会自动传播,因此您不能错误地 检查错误情况。因为有时候,你到(从一个构造函数中),所以为什么不一致。 C ++根本不提供任何更好的信号错误条件的方法。

It's very simple. Avoid error codes like fire and prefer exceptions, unless error code really makes more sense in an individual case. Why? Because exceptions can carry a lot more information — see e.g. Boost.Exception. Because they propagate automatically, so you can't make a mistake of not checking for error condition. Because sometimes, you have to (bailing out of a constructor), so why not be consistent. C++ simply does not offer any better way for signalling error conditions.

另一方面,Asserts用于完全不同的东西 - 验证代码的内部状态,假设应该总是成立。

Asserts, on the other hand, are used for something completely different — verifying internal state of the code, and assumptions that should always hold true. Failed assertion is always a bug — whereas exception might signal invalid external input, for example.

对于链接的指南:忘记Google风格指南甚至存在,它是可怕的,而且这不只是我的意见。 LLVM - 可执行的大小几乎不重要,它不是真的你应该浪费时间思考。 Qt - Qt缺乏异常安全,但这并不意味着你的代码也。使用现代做法,并且安全的例外不应太难。

As for linked guides: forget Google style guide even exists, it's simply terrible, and it's not just my opinion. LLVM — executable size hardly matters, it's not really something you should waste time thinking about. Qt — Qt is lacking in exception safety, but that doesn't mean your code has to, too. Use modern practices, and being exception safe shouldn't be too hard.

这篇关于C ++错误代码vs ASSERTS VS异常选择选择:(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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