【问题标题】:Error checking on many function calls对许多函数调用进行错误检查
【发布时间】:2012-04-26 20:18:49
【问题描述】:

有时当我使用 C++/C 编程时,我最终会多次调用同一个函数,我想知道检查所有这些调用的错误的最有效方法是什么?使用if else 语句会占用大量代码并且看起来很难看。我想出了自己检查错误的方法,也许有更好的方法可以使用。

int errs[5] = {0};
errs[0] = functiona(...);
errs[1] = functiona(...);
...
errs[5] = functiona(...);
for (int i = 0; i < 5; i++)
{
  if (err[i] == 0)
     MAYDAY!_wehaveanerror();
}

注意:我知道使用 trycatch 对于 C++ 可能会更好,因为它会通过在第一个错误时抛出异常来解决这个问题,但问题是它与很多不兼容返回错误代码的函数,例如 Windows API。谢谢!

【问题讨论】:

  • 为什么您不想在发生错误时尽快知道,并在收到时检查每个返回码?使用您的方法,您可能会产生一系列错误,并且只会找出第一个错误。
  • 迭代您将传递的参数列表,使用该迭代的参数调用函数,在第一次检测到错误时将“我们有错误”布尔设置为 true,然后终止循环那时和那里。
  • 另外:仅仅因为Windows API函数只返回错误代码并不意味着你不能在收到错误代码时抛出。
  • @ScottHunter 我想这样做,这就是为什么我要问我的问题是如何找到一种巧妙的方法来做到这一点。我的代码调用相同的函数GetPrivateProfileStringA 大约 40 次来加载我的程序的设置。这意味着有 40 个 if 语句和 40 个 then 语句加上非常丑陋的处理程序。
  • @user99545:注意:提供此功能只是为了与基于 Windows 的 16 位应用程序兼容。应用程序应在注册表中存储初始化信息。

标签: c++ c recursion error-handling


【解决方案1】:

你可以像这样写一些伪 C++:

struct my_exception : public std::exception {
    my_exception(int); /* ... */ };

int main()
{
    try
    {
        int e;
        if ((e = function()) != SUCCESS) { throw my_exception(e); }
        if ((e = function()) != SUCCESS) { throw my_exception(e); }
        if ((e = function()) != SUCCESS) { throw my_exception(e); }
    }
    catch (my_exception & e)
    {
        std::cerr << "Something went wrong: " << e.what() << "\n";
    }
}

【讨论】:

  • 或者您可以用一个将错误代码转换为异常的薄层来包装 Windows API 函数。 Borland Delphi 在 15 年前就已经做到了这一点,效果很好,现在仍然很有效。
【解决方案2】:

如果...如果函数有机会抛出不同的错误,您还应该添加一个 catch all。

struct my_exception : public std::exception {
    my_exception(int); /* ... */ };

int main()
{
    try
    {
        int e;
        if ((e = function()) != SUCCESS) { throw my_exception(e); }
        if ((e = function()) != SUCCESS) { throw my_exception(e); }
        if ((e = function()) != SUCCESS) { throw my_exception(e); }
    }
    catch (my_exception & e)
    {
        std::cerr << "Something went wrong: " << e.what() << "\n";
    }
    catch (...)
    {
        //Error Checking
    }
}

【讨论】:

    【解决方案3】:

    如何处理函数中的检查?

    void my_function() {
      if (!create_window())
        throw Error("Failed to create window");
    }
    
    int main() {
      try {
        my_function();
      } catch (const Error& e) {
        cout << e.msg << endl;
      } catch (...) {
        cout << "Unknown exception caught\n"
      }
    
      return 0;
    }
    

    【讨论】:

      【解决方案4】:

      如果您一遍又一遍地调用同一个函数,最简洁的方法可能是使用宏。我会建议类似:

      #define CHECKERROR(x) if(x == 0) wehaveanerror()
      
      CHECKERROR(function(...));
      CHECKERROR(function(...));
      

      显然,此宏将非常特定于所涉及的特定函数和错误处理程序,因此在这些调用之后undef 它可能是谨慎的。

      【讨论】:

        【解决方案5】:

        做得更老派,但保留原始错误响应,但一旦发生错误就会响应而不会看起来很丑:

        #define callcheck(r) if ((r)==0) MAYDAY!_wehaveanerror()
        
        callcheck(functiona(...));
        callcheck(functiona(...));
        ...
        

        【讨论】:

          猜你喜欢
          • 2018-02-12
          • 2016-08-09
          • 2017-04-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-05-18
          • 1970-01-01
          相关资源
          最近更新 更多