在C ++中使用try / catch块 [英] Usage of try/catch blocks in C++

查看:124
本文介绍了在C ++中使用try / catch块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般来说,我倾向于对具有多个失败点的代码使用try / catch,其中失败有一个共同的处理程序。



根据我的经验,这是典型的代码,在执行某些操作或执行某些操作后输出之前,输入或上下文是合格的。



我收到了文学和同事的律师,尽量减少这些块中的代码,我接受作为一般好的建议。



我想了解一下更多关于上述建议的基础:




  • 开销的性质是什么?

  • 有没有最新的开发指南来解决try / catch块的推荐使用(或回避)?

  • 更快的处理器和更现代的编译器如何缓解try / catch的问题? li>


感谢您的帮助,



AJ

解决方案

在c ++中,成本取决于实现。一般来说,有两种方法来实现异常:



第一种是表方法。编译器构建一组表来查找,在抛出异常的地方,去哪里。当抛出异常时,它必须在每个表上搜索调用堆栈,直到找到将捕获此异常的东西。由于这是基于运行时,输入或退出try catch不会产生惩罚(好),但抛出异常涉及潜在的许多查找,产生更慢的抛出。我个人更喜欢不必付费的try catch块,因为异常应该是一个非常罕见的情况。这也会使可执行文件更大,如果他们必须存储表。



秒是代码方法。每次代码进入try catch块,在概念上,块的位置被推送到堆栈。这在进入和退出try-catch块时产生成本,然而,当抛出异常时,运行时机制可以快速地弹出堆栈以找到去哪里。所以,抛出异常是(更多)更快,但进入块现在有一个成本。将try catch块放在严格的低级别循环中可能会产生显着的开销。



您必须检查您的特定编译器以查看它们使用的编译器。

In general, I tend to use try/catch for code which has multiple failure points for which the failures have a common handler.

In my experience, this is typically code which qualifies input or context before performing some action or output after performing some action.

I have received counsel from literature and colleagues to minimize the code in such blocks and I accept that as generally good advice.

I would like to understand a bit more about the foundation for the above advice:

  • What is the nature of the overhead?
  • Are there recent development guidelines that address the recommended usage (or avoidance) of try/catch blocks?
  • How much do faster processors and more modern compilers mitigate the problems with try/catch?

Thanks in advance for the help,

AJ

解决方案

In c++, the cost depends on the implementation. In general, there are two ways to implement exceptions:

The first is the "table" approach. The compiler builds a set of tables to look up, at the point where the exception is thrown, where to go. When the exception is thrown, it has to search through each table up the call stack until it finds something that will catch this exception. Since this is all runtime based, entering or exiting a try catch produces no penalty (good) but throwing an exception involves potentially many lookups, producing a much slower throw. I personally prefer the not-having-to-pay for try catch blocks, because exceptions should be a very rare circumstance. This also would make executables larger, if they have to store the tables.

The seconds is the "code" approach. Each time the code enters a try catch block, conceptually, the location of the block is pushed onto a stack. This produces a cost during entering and exiting a try-catch block, however, when an exception is thrown, the runtime mechanism can quickly pop off the stack to find where to go. So, throwing exceptions is (much?) faster, but entering a block now has a cost. Putting a try catch block in a tight low level loop could produce significant overhead.

You would have to check your specific compiler to see which one they use.

这篇关于在C ++中使用try / catch块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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