通过名称cpp捕获特定的异常

时间:2018-06-29 07:45:04

标签: c++ exception

我使用pcl 1.7 tracking code,在某些情况下使用此方法:

tracker_->compute ();

给我这些错误:

  

[pcl :: ApproxNearestPairPointCloudCoherence :: initCompute] PointCloudCoherence :: Init失败。       [pcl :: ApproxNearestPairPointCloudCoherence :: compute]初始化失败。

我现在知道抛出了错误,并且我想准确地捕获此错误,以告诉程序如果发生此错误该怎么办。我这样尝试过:

  try
  {
    tracker_->compute ();
  }
  catch (...)
  {
    std::cout<<"inside the exception"<<std::endl;
  }

要捕获所有错误,但是它不起作用.....以及

catch(pcl::ApproxNearestPairPointCloudCoherenceException &e) 

我的程序甚至没有编译。...

那么有什么主意我做错了吗?

1 个答案:

答案 0 :(得分:2)

我浏览了the source以查找引发错误的位置,found out在您的特定情况下是由以下原因引起的

if (!PointCloudCoherence<PointInT>::initCompute ())
{
    PCL_ERROR ("[pcl::%s::initCompute] PointCloudCoherence::Init failed.\n", getClassName().c_str());
    return (false);
}

因此,您看到的是,这里没有throw xxx,只有一个对我们隐藏了实际的错误处理。


要弄清楚到底发生了什么,我搜索了PCL_ERROR的定义,搜索得出该定义在文件print.h中的定义为

#define PCL_ERROR(...)   pcl::console::print (pcl::console::L_ERROR, __VA_ARGS__)

pcl::console::print是一个函数,只需 打印 错误,而不是引发异常。


因此,您无法捕获该错误,因为它不是例外,而只能是写入控制台的纯文本。