PCL中的异常机制

 PCL在编写和应用的过程中,为了提高程序的稳健性,PCL提供了一套自己的异常处理机制。
 PCL中的异常处理方式,依赖于内部自定的异常基类PCLException,它继承C++的std::runtime_error类。

/** \class PCLException
  * \brief A base class for all pcl exceptions which inherits from std::runtime_error
  */
class PCLException : public std::runtime_error{
	public:
 		PCLException (const std::string& error_description,
 		              const char* file_name = NULL,
 		              const char* function_name = NULL,
 		              unsigned line_number = 0)
 		              : std::runtime_error (createDetailedMessage (error_description,
 		                                                           file_name,
 		                                                           function_name,
 		                                                           line_number))
        , file_name_ (file_name)
        , function_name_ (function_name)
        , line_number_ (line_number)
		{}
     	// 其余成员函数和成员变量省略......
}

 PCL中实现的异常类(例如:IOException、InitFailedException、KernelWidthTooSmallException等)均是继承PCLException类。若我们自己也需要实现一个异常类,也可以继承该基类。
 此外,为了方便开发这使用自定义的异常类,PCL中提供了一个宏,方便开发者调用,如下所示:

/** PCL_THROW_EXCEPTION a helper macro to be used for throwing exceptions.
  * This is an example on how to use:
  * PCL_THROW_EXCEPTION(IOException,
  *                     "encountered an error while opening " << filename << " PCD file");
  */
#define PCL_THROW_EXCEPTION(ExceptionName, message)                        \
{                                                                           \
  std::ostringstream s;                                                     \
  s << message;                                                             \
  throw ExceptionName(s.str(), __FILE__, BOOST_CURRENT_FUNCTION, __LINE__); \
}

 异常抛出宏是有了,那么该怎么去捕获PCL中的异常呢?我们可以用try…catch程序块进行处理,如下所示:

try{
	// 调用PCL API的地方
}
catch (pcl::PCLException& e){
	// 捕获PCL异常信息,并进行相应的处理
	std::cout << "pcl::PCLException: " << e.what() << std::endl;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值