c++ - 米斯拉-C++ :2008[8-4-3] : return in all exit path in function

标签 c++ return throw misra

在测试我的代码(静态分析)以查看我是否尊重 misra c++ 2008 时,我收到以下错误

Function does not return a value on all paths.

函数看起来像

int* Dosomething(string v)
{
   int* retvalue = NULL;

   if( 0 == exists(v) )
   {
      throw("error: value doesn't exist");
   }
   else
   {
     retvalue = dosomecomputations(v);
   }

   return retvalue;
}

我真的需要抛出一个异常,因为调用者应该根据错误做一些事情。可能的错误列表可能很大,而且不仅仅是该代码示例中的值不存在。

我该如何管理它?我认为在这种情况下,我使用的工具不应将其视为不符合 misra。

感谢您的建议。

罗尼。

最佳答案

以下代码不应报告任何应用了 MISRA C++ 2008 规则的警告/错误。所以很可能是您的工具有问题 - 或者发布的代码不是受影响的部分。

#include <string>

int exists(std::string v){ (void)v; return 1; }
int* dosomecomputations(std::string v){ (void)v; return NULL; }

int* dosomething(std::string v){
  int* retvalue = NULL;
  if( 0 == exists(v) ){
    throw("error: value doesn't exist");
  }else{
    retvalue = dosomecomputations(v);
  }
  return retvalue;
}

尝试使用您的 MISRA 检查器检查上面的代码片段,看看它是否仍在报告任何内容。如果问题仍然存在,我会联系工具供应商并询问他这个问题。

关于c++ - 米斯拉-C++ :2008[8-4-3] : return in all exit path in function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20425065/

相关文章:

swift - 从闭包中抛出错误

c# - 哪种类型的可抛出错误适用于这种情况?

c++ - 什么会导致返回函数崩溃? C++

python - 重新抛出 python 异常。抓哪个?

c++ - 解决在 C++ 中调用具有相同名称的函数的方法中的冲突

c++ - C++:返回具有继承对象的基于范围的循环迭代器

java - 返回 null 的方法正在操作最终 int 数组

button - 按下按钮后返回一个彩色框

c++ - 嵌套的 catch 运算符

c++ - C++ 中的单元测试