Orbbec SDK C++ API 使用 異常機制 實(shí)現接口調用狀態(tài)反饋,當用戶(hù)應用代碼調用 API 執行過(guò)程中出現問(wèn)題,會(huì )向外拋出 ob::Error 類(lèi)型異常。
ob::Error 是 Orbbec SDK 自定義的一個(gè)異常類(lèi),其提供了 4 個(gè)接口可以幫助用戶(hù)在發(fā)生異常時(shí)判斷異常的原因:
// 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. 推薦用戶(hù)應用代碼在調用 Orbbec SDK C++ API 時(shí),通過(guò) 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. 如果函數內包含多個(gè) 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);
}
