简体   繁体   English

处理C ++错误

[英]Handle errors C++

So, I have a simple library-class and this class has some methods that return some values like code errors. 因此,我有一个简单的库类,该类具有一些返回一些值(如代码错误)的方法。

User_program User_program

MyClass go(arg1, arg2)
if(go.execute() == 0)
  std::cout << go.result();

And my class has something like this 我班上有这样的事情

My class 我的课

int execute()
{

    if((temp = doBar()) != 0)
    {
         return temp;
    }
    return SUCCESS;
}

int doBar()
{
   if(foo == 1)
      return DIVIDION_BY_ZERO;
   if(fzz == 0)
      return OPERATION_ERROR;
}

And so on. 等等。 So, is there any method to make errors more helpful, I've heard about enum with const for errors, but I don't understand how to implement it. 因此,有没有什么方法可以使错误更有用,我听说过用const枚举错误的枚举,但是我不知道如何实现。

Thanks. 谢谢。

Not sure that I understood the question right, but here is few moments. 不确定我是否理解正确的问题,但这是片刻。

  1. In your case enum`s is way to store all definitions of const values like (SUCCESS, DIVIDION_BY_ZERO, etc) in one place (even in one translation unit). 在您的情况下,枚举是一种将const值的所有定义(如(SUCCESS,DIVIDION_BY_ZERO等))存储在一个位置(甚至在一个转换单元中)的方法。 And also compiletime validation of types. 以及类型的编译时验证。 read more here: [1] 在这里阅读更多: [1]
  2. 2) If intresting how implemented some error check there is no need to go far. 2)如果了解如何执行某些错误检查,则无需走太远。

    • First of all look at C handling errors in libc [2] 首先看一下libc中的C处理错误[2]
    • In ISO C++11 presented [system_error] 在ISO C ++ 11中提出[system_error]
    • And typical error handling in libs released special for (almost) each type like in Qt [QNetworkReply] 并且在Qs [QNetworkReply]中针对(几乎)每种类型发布了特殊的库中的典型错误处理。
  3. And also using exceptions(and dark side of C++ like RTTI) in libs is bad idea. 而且在libs中使用异常(以及RTTI等C ++的黑暗面)也是个坏主意。 But take this link too [3] 但也请使用此链接[3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM