C ++ - 返回代码异常的参数 [英] C++ - Arguments for Exceptions over Return Codes

查看:78
本文介绍了C ++ - 返回代码异常的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在讨论一个新的C ++项目中的哪种方式。我赞成退货代码的例外情况(仅用于例外情况),原因如下:


  1. 构造函数不能返回代码

  2. 从更清洁的逻辑代码中解除失败路径(这应该非常罕见)脱离

  3. 在非特殊情况下更快(不检查if / else数十万次)

  4. 如果有人拧回返回代码设置(忘记返回FAIL),可能需要很长时间才能追踪。

  5. 从错误中包含的消息中获取更好的信息。 (有人向我指出,返回枚举可以做同样的错误代码)

  6. 从Jare​​d Par 不可能忽略没有专门设计为处理的代码它

这些是我想到的和谷歌搜索的点数。在过去几年中,我必须承认倾向于在C#中工作的例外。请提出进一步的理由使用退货代码之外的例外。对于那些喜欢退货代码的人,我也愿意听你的推理。谢谢

解决方案

我认为这个

使用例外的参数


  1. 例外是从正常程序流程中分离错误处理代码,从而使代码更易读,更强大和可扩展。

  2. 投掷一个例外是从构造函数报告错误的唯一干净的方法。

  3. 与错误代码不同,异常很难忽略。

  4. 异常很容易从深度嵌套的函数传播。

  5. 异常可以是,通常是用户定义的类型,比错误代码携带更多的信息。

  6. 异常对象通过使用类型系统与处理程序相匹配。

使用例外的参数


  1. 异常通过创建多个不可见的退出点来破解代码结构,使得代码难以阅读和检查。

  2. 异常很容易导致资源泄露,特别是在没有内置的垃圾回收器,最后阻止。

  3. 学习写异常安全代码很难。

  4. 例外是昂贵的,打破了承诺只支付

  5. 异常很难引入旧版代码。

  6. 异常很容易被滥用来执行属于正常程序流的任务。


I'm having a discussion about which way to go in a new C++ project. I favor exceptions over return codes (for exceptional cases only) for the following reasons -

  1. Constructors can't give a return code
  2. Decouples the failure path (which should be very rare) from the logical code which is cleaner
  3. Faster in the non-exceptional case (no checking if/else hundreds of thousands of times)
  4. If someone screws up the return code settings (forgets to return FAIL) it can take a very long time to track down.
  5. Better information from the message contained in the error. (It was pointed out to me that a return enum could do the same for error codes)
  6. From Jared Par Impossible to ignore without code to specifically designed to handle it

These are the points I've come up with from thinking about it and from google searches. I must admit to being predisposed to exceptions having worked in C# for the past couple of years. Please post further reasons for using exceptions over return codes. For those who prefer return codes, I would also be willing to listen to your reasoning. Thanks

I think this article sums it up.

Arguments for Using Exceptions

  1. Exceptions separate error-handling code from the normal program flow and thus make the code more readable, robust and extensible.
  2. Throwing an exception is the only clean way to report an error from a constructor.
  3. Exceptions are hard to ignore, unlike error codes.
  4. Exceptions are easily propagated from deeply nested functions.
  5. Exceptions can be, and often are, user defined types that carry much more information than an error code.
  6. Exception objects are matched to the handlers by using the type system.

Arguments against Using Exceptions

  1. Exceptions break code structure by creating multiple invisible exit points that make code hard to read and inspect.
  2. Exceptions easily lead to resource leaks, especially in a language that has no built-in garbage collector and finally blocks.
  3. Learning to write exception safe code is hard.
  4. Exceptions are expensive and break the promise to pay only for what we use.
  5. Exceptions are hard to introduce to legacy code.
  6. Exceptions are easily abused for performing tasks that belong to normal program flow.

这篇关于C ++ - 返回代码异常的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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