处理代码中的错误时,通过调用链返回入口点仅 return 单个 "error" 值是否可以接受?

When handling errors in code, is it acceptable to just return a single "error" value through the call chain back to the entry point?

我 运行 在我的代码中遇到了几个实例,其中有效的用户输入对于正在执行的操作无效(例如为因正在接受培训而没有时间表的员工输入时间表遵守条目).这是一个允许的行为,但可以理解的是,在计算进度遵守报告时会在路上产生错误。在此示例中,如果在执行依从性条目时找不到该员工的时间表,则会抛出错误。

在这种情况下,我通常会 return 一个对象,它封装了条目和时间表上的一些基本数据。如果出现这样的错误,我 return -1,并且所有后续方法只检查 -1,如果找到,那么它们也会 return -1执行他们通常的操作。一直回到调用链的开头,预计不再有 return 并且不会对 -1 值采取任何最终操作。

这是处理代码错误的合适模式吗?如果不是,处理这些问题的最佳方式是什么?

这是一种方法,但也有缺点:

  • 你需要每个函数来处理错误
  • 您在处理错误时缺少任何上下文(您不知道调用函数中发生错误的原因)

更好的方法是使用大多数语言都具有的 exception/try-catch 机制。它允许您处理所需函数中的错误(如果需要,甚至可以在顶部),并添加上下文。它还更具可扩展性、健壮性和可读性(阅读代码的每个人都明白异常是什么)。

我同意 DevShark 的回答:对于 scalability/readability/robustness,您需要将错误值与其上下文分开。

这里通过返回 -1 将错误上下文编码在值中,只要错误被捕获就没问题 "soon".

如果应该检索其他错误上下文怎么办?

您必须 "bind" 另一个禁止值(-2、-3、...) 您将在许多地方以重复的错误逻辑处理结束

if( error == -1 )
   // treat exception -1 here
if( error == -2 )
   // treat exception -2 + alert top component
   return error
else 
   // error is undefined
   return error; 

如果-1成为沿路可接受的值会怎样?

然后您必须将错误转化为其他可能具有误导性的内容。

怎么办?

正如 Devshark 所述,try/catch 异常很好,实际上是为处理错误情况而设计的;然而,根据您的应用程序约束(实时,...)或编程领域(嵌入式,...),使用配对技术实现此功能将是 simpler/lighter,例如:

// ErrorType could be an enum describing the context
// ValueType could be what you want from scalar type to pointer to more complex structures
std::pair<ErrorType, ValueType>

// For instance a typical function returning bool error and int value
std::pair<bool, int> foo( ) 
{
   std::pair<bool, int> res;

   // handle your task - get the new value
   int newVal = foo2();

   if( errorOccurs ) 
   {
      res.first = false; // flag indicating error
      res.second = -1; // value can be made invalid or not
   } else {
      res.first = true;
      res.second = newVal;
   }

   return res;

}