C++ 为什么要捕获异常作为对常量的引用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2145147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:15:51  来源:igfitidea点击:

Why catch an exception as reference-to-const?

c++exceptionconst

提问by cairol

I've heard and read many times that it is better to catch an exception as reference-to-const rather than as reference. Why is:

我多次听说并阅读过,最好将异常作为对常量的引用而不是作为引用来捕获。为什么是:

try {
    // stuff
} catch (const std::exception& e) {
    // stuff
}

better than:

优于:

try {
    // stuff
} catch (std::exception& e) {
    // stuff
}

采纳答案by Kornel Kisielewicz

You need:

你需要:

  • a reference so you can access the exception polymorphically
  • a const to increase performance, and tell the compiler you're not going to modify the object
  • 一个引用,以便您可以多态地访问异常
  • 一个 const 来提高性能,并告诉编译器你不会修改对象

The latter is not as muchimportant as the former, but the only real reason to drop const would be to signal that you want to do changes to the exception (usually useful only if you want to rethrow it with added context into a higher level).

后者不是尽可能多的重要,因为前者,但唯一的真正原因放弃常量将表明你想要做的更改例外(仅当你想添加的背景下重新抛出它变成一个更高的水平通常为有用) .

回答by Lightness Races in Orbit

There is basically no reason at all.

基本上没有任何理由。

Exception objects live in their own memory spaceso you don't have to worry about catching exceptions created in temporary expressions.

异常对象存在于它们自己的内存空间中†,因此您不必担心捕获在临时表达式中创建的异常。

All you're doing is promising that you won't modify the exception object, but since exception objects should have an immutable interface, there is really nothing practical here.

您所做的只是承诺您不会修改异常对象,但是由于异常对象应该有一个不可变的接口,因此这里实际上没有任何实际意义。

However, it might make you feel warm and cosy when you read it — that's how it is for me!

然而,当你阅读它时,它可能会让你感到温暖和舒适——这对我来说就是如此!

They have their own, special, thread-local stack.
Disclaimer:Boost.Exception breaks this in order to do funky stuff and add exception details, post-construction. But this is hackery!

他们有自己的、特殊的、线程本地堆栈。
免责声明:Boost.Exception 打破了这一点,以便在构建后做一些时髦的事情并添加异常详细信息。但这是黑客!

回答by Warpin

It tells the compiler that you won't be calling any function which modify the exception, which may help to optimize the code. Probably doesn't make much of a difference, but the cost of doing it is very small too.

它告诉编译器你不会调用任何修改异常的函数,这可能有助于优化代码。可能没有太大区别,但这样做的成本也非常小。

回答by matt

are you going to modify the exception? if not, it may as well be const. same reason you SHOULD use const anywhere else (I say SHOULD because it doesn't really make that much difference on the surface, might help compilers, and also help coders use your code properly and not do stuff they shouldn't)

你要修改异常吗?如果不是,它也可能是 const。同样的原因,您应该在其他任何地方使用 const(我说应该是因为它在表面上并没有真正产生太大的不同,可能有助于编译器,还可以帮助编码人员正确使用您的代码,而不是做他们不应该做的事情)

exception handlers, may be platform specific, and may put exceptions in funny places because they aren't expecting them to change?

异常处理程序,可能是特定于平台的,并且可能将异常放在有趣的地方,因为他们不希望它们改变?

回答by matt

For the same reason you use a const.

出于同样的原因,您使用 const。