AG·尊龙凯时 - 人生就是搏!AG·尊龙凯时

AG·尊龙凯时 - 人生就是搏!AG·尊龙凯时

如何处理运行错误

概述

Orbbec SDK C++ API 使用 异常机制 实现接口调用状态反馈,当用户应用代码调用 API 执行过程中出现问题,会向外抛出 ob::Error 类型异常。

使用说明

ob::Error 是 Orbbec SDK 自定义的一个异常类,其提供了 4 个接口可以帮助用户在发生异常时判断异常的原因:

// libobsnesor/hpp/Error.hpp

/**
* @brief Get the detailed error logs of SDK internal exceptions.
* @return A C-style string containing the error message.
*/
const char *getMessage() const noexcept;

/**
* @brief Get the exception type of the error, which can be used to determine which module is abnormal.
* @return The OBExceptionType enum value.
*/
OBExceptionType getExceptionType() const noexcept;

/**
* @brief Get the name of the error function.
* @return A C-style string containing the name of the error function.
*/
const char *getName() const noexcept;

/**
* @brief Get the parameter passed to the error interface.
* @return A C-style string containing the error interface parameter.
*/
const char *getArgs() const noexcept;

使用方法如下:

1. 推荐用户应用代码在调用 Orbbec SDK C++ API 时,通过 try-catch 机制捕获并处理异常:

try{
  pipe.start(config);
}catch(ob::Error &e) {
  std::cerr << "Failed to start the pipeline!" << " Exception type:" << e.getExceptionType() << ", name:" << e.getName() << ", message:" << e.getMessage() << std::endl;
}

2. 如果函数内包含多个 Orbbec SDK C++ API 调用,可以在函数体外部直接捕获所有异常:

int main(int argc, char **argv) try {
  // Print the sdk version number, the sdk version number is divided into major version number, minor version number and revision number
  std::cout << "SDK version: " << ob::Version::getMajor() << "." << ob::Version::getMinor() << "." << ob::Version::getPatch() << std::endl;
  // Print sdk stage version
  std::cout << "SDK stage version: " << ob::Version::getStageVersion() << std::endl;

  // Create a Context.
  ob::Context ctx;

  // Query the list of connected devices
  auto devList = ctx.queryDeviceList();

  // Get the number of connected devices
  if(devList->deviceCount() == 0) {
    std::cerr << "Device not found!" << std::endl;
    return -1;
  }

  // Create a device, 0 means the index of the first device
  auto dev = devList->getDevice(0);

  return 0;
}
catch(ob::Error &e) {
  std::cerr << "function:" << e.getName() << "\nargs:" << e.getArgs() << "\nmessage:" << e.getMessage() << "\ntype:" << e.getExceptionType() << std::endl;
  exit(EXIT_FAILURE);
}

异常类型 

image.png


AG·尊龙凯时