为什么C ++允许抛出任何东西? [英] Why does C++ allow to throw anything?

查看:70
本文介绍了为什么C ++允许抛出任何东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚看到了这个问题及其相关答案.考虑到我以前从未遇到过使用 throw s的这种方式,我很惊讶地发现这甚至是可能的.

I just saw this question and its related answers. Considering I never ran into this way of using throws before, I was quite surprised to find out this is even possible.

  • 允许(几乎)扔掉和抓住任何东西背后的逻辑是什么?
  • 是否使用了超越异常/错误信号的throw-catch语法?如果是这样,是被认为是不好的做法还是我从未意识到的通用"用法?

推荐答案

不加推测就很难回答,但是Bjarne 1998年的论文在描述异常处理时使用任意类型,并建议为方便/语义创建所述类型的层次结构.似乎一开始他就没有想到 exception 的基类".

This is hard to answer without speculation, but Bjarne's 1998 paper "An Overview of the C++ Programming Language" uses arbitrary types when describing exception handling, and suggests creating a hierarchy of said types for convenient/semantics. It would seem that he did not have a "base class" of exception in mind for any of these in the beginning.

具有标准层次结构(基于 std :: exception 的概念)的生命可能是作为加法"开始的,这是一种接近Bjarne建议的使用异常的简便方法,而不是每个人都应使用异常的基础.当代的做法是从 std :: exception 派生所有异常,似乎以后会出现.

It's possible that the notion of having a standard hierarchy (based on std::exception) began life as an "addition", a convenient way to approach Bjarne's suggested use of exceptions, rather than the building-blocks on which everyone's use of exceptions should be based. That contemporary practice is to derive all exceptions from std::exception would seem to have come along later.

如今,我想不出一个很好的理由不这样做,除非出于其他原因,除了那些使用您的代码的人可能会期望顶级的 catch(const std :: exception&)代码>吸取任何有用的东西.我也倾向于将 catch(...)放在 main 中,以防万一.

Nowadays, I can't think of a good reason not to do that, if for no other reason than people using your code will probably expect a top-level catch (const std::exception&) to suck up anything useful. I do also tend to put a catch (...) in main, though, just in case.

更实际地说,如果不是这种情况,则必须有其他规则来约束 throw ,使其仅对从 std :: exception派生的类型的表达式有效",这似乎并没有任何足以证明其他规则合理的实际好处.不管您信不信,C ++源自一个极简主义的问题:为什么我们没有这样的规则?",尽管显然它的膨胀似乎与此矛盾.这些年来.

Speaking more practically, if this weren't the case, there would have to be additional rules constraining throw to only be "valid" on expressions with type deriving from std::exception, which would not seem to have any real-world benefit sufficient to justify the additional rules. C++, believe it or not, comes from a place of minimalism in terms of the question "why don't we have such-and-such a rule", although obviously its bloat would seem to contradict this after all these years.

我怀疑这与将 throw 用于非例外情况有关,因为这一直被认为是不良做法.Bjarne描述此功能的方式是:

I doubt this has anything to do with using throw for non-exceptional things, because that has always been considered bad practice. The way Bjarne describes this feature is:

异常用于将控制权从检测到错误的位置转移到对处理此类错误表现出浓厚兴趣的某些调用方.显然,这是仅应用于无法在本地处理的错误的机制.[..]异常可用于使错误处理更加规范化和常规.

Exceptions are used to transfer control from a place where an error is detected to some caller that hasexpressed interest in handling that kind of errors. Clearly, this is a mechanism that should be used only for errors that cannot be handled locally. [..] Exceptions can be used to make error handling more stylized and regular.

因此很明显,至少最初的设计意图(同样,这仍然是常识)是仅将异常用于异常/错误情况.

So it's clear that at least the original design intent (and, again, this is still common wisdom) is to use exceptions for exceptional/errory cases only.

这篇关于为什么C ++允许抛出任何东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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