为什么 c++ try-catch 块不能在 ubuntu 上与 g++ 一起使用?
Why don't c++ try-catch blocks work with g++ on ubuntu?
我为我的工作项目制作了一个QT测试应用程序(一个带有GUI的应用程序,用于测试我制作的一些功能),但我的异常根本不起作用,我不明白为什么。也许我遗漏了什么,但代码对我来说似乎是正确的,这里有一个示例:
抛出异常的函数(在我的测试中,它抛出std::invalid_argument
):
std::vector<pcl::PointCloud<pcl::PointXYZRGB>::Ptr> cloud_manip::fragment_cloud(
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_ptr, float max_scaled_fragment_depth)
{
if (!cloud_ptr)
{
throw invalid_cloud_pointer();
}
if ((aux::cmp_floats(max_scaled_fragment_depth, 0.00, 0.005)) || (max_scaled_fragment_depth < 0))
throw std::invalid_argument("Invalid max fragment depth.");
float curr_depth = FLT_MAX;
std::vector<pcl::PointCloud<pcl::PointXYZRGB>::Ptr> cloud_fragments;
for (unsigned int cloud_it = 0; cloud_it < cloud_ptr->points.size(); cloud_it++)
{
// end of a fragment
if ((cloud_ptr->points[cloud_it].y > (curr_depth + max_scaled_fragment_depth))
|| (cloud_ptr->points[cloud_it].y < (curr_depth - max_scaled_fragment_depth)) )
{
curr_depth = cloud_ptr->points[cloud_it].y;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr new_cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
cloud_fragments.push_back(new_cloud);
}
// filling current cloud
else
(cloud_fragments.back())->points.push_back(cloud_ptr->points[cloud_it]);
}
return cloud_fragments;
}
捕获异常的第一个函数:
pcl::PointCloud<pcl::PointXYZRGB>::Ptr fast_normal_estimation(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_ptr, int max_neighbs,
float radius, float x_scale, float y_scale, float z_scale, float max_fragment_depth)
{
try
{
// the cloud colored by its normal vectors; return value
pcl::PointCloud<pcl::PointXYZRGB>::Ptr colored_cloud_ptr;
float max_scaled_fragment_depth = max_fragment_depth / y_scale;
cloud_manip::scale_cloud(cloud_ptr, x_scale, y_scale, z_scale); // scaling cloud
std::vector<pcl::PointCloud<pcl::PointXYZRGB>::Ptr> cloud_fragments =
cloud_manip::fragment_cloud(cloud_ptr, max_scaled_fragment_depth); // fragmenting cloud for less execution time
// estimating the normals for each cloud fragment in parallel
// #pragma omp parallel for schedule(static)
for (unsigned int i = 0; i < cloud_fragments.size(); i++)
{
normal_estimation(cloud_fragments[i], radius, max_neighbs);
}
colored_cloud_ptr = cloud_manip::merge_clouds(cloud_fragments); // merging fragments to build original cloud
cloud_manip::scale_cloud(colored_cloud_ptr, (1.0/x_scale), (1.0/y_scale), (1.0/z_scale)); // restoring widop scale
return colored_cloud_ptr;
}
catch (const std::logic_error& le)
{
throw le;
}
}
测试功能:
void test_normal_estimation(std::string import_path, std::string export_path, float radius,
int max_neighbs, float x_scale, float y_scale, float z_scale,
float max_fragment_depth)
{
try
{
pcl::PointCloud<pcl::PointXYZRGB>::Ptr base_cloud;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr colored_cloud; // output cloud
base_cloud = cloud_io::import_cloud(import_path);
colored_cloud = fast_normal_estimation(base_cloud, max_neighbs, radius, x_scale, y_scale, z_scale,
max_fragment_depth);
cloud_io::export_cloud(export_path + "normal_estimation_test_" + boost::lexical_cast<std::string>(radius) + "_"
+ boost::lexical_cast<std::string>(max_neighbs) + "_" + boost::lexical_cast<std::string>(x_scale) + "_"
+ boost::lexical_cast<std::string>(y_scale) + "_" + boost::lexical_cast<std::string>(z_scale) + "_"
+ boost::lexical_cast<std::string>(max_fragment_depth) + ".txt", colored_cloud);
}
catch(const std::logic_error& le)
{
throw le;
}
}
最后是应该显示错误消息的界面:
void normal_estimation_test_form::on_launch_test_btn_clicked()
{
// for when the test is done
QMessageBox done;
this->setEnabled(false);
_ned->radius = ui->radius_dsb->value();
_ned->max_neighbs = ui->max_neighbs_sb->value();
_ned->x_scale = ui->x_scale_dsb->value();
_ned->y_scale = ui->y_scale_dsb->value();
_ned->z_scale = ui->z_scale_dsb->value();
_ned->max_fragment_depth = ui->max_fragm_depth_sb->value();
try
{
test_normal_estimation(_ned->cloud_in_path, _ned->cloud_out_path, _ned->radius,
_ned->max_neighbs, _ned->x_scale, _ned->y_scale,
_ned->z_scale, _ned->max_fragment_depth);
done.setText("Cloud normal estimation test completed.");
done.exec();
}
catch (const std::logic_error& le)
{
QErrorMessage q_err_msg;
QString err_msg;
err_msg.append("Invalid input.");
q_err_msg.showMessage(err_msg, "Input Error");
}
}
你知道为什么我的异常根本没有被发现吗?提前谢谢。
edit_1:我知道我没有捕获std::invalid_argument
,但这是因为根据cplusplus,它是std::logic_error
的一个子类。
我已经找到了这个问题的答案,并在这里进行了解释:Qt中的异常处理。这看起来不是一种合法的方式,但我不知道还有什么更好的方法。
相关文章:
- 如何将enable-if与模板参数和参数包一起使用
- 如何将PERF_AMPLE_READ与mmap一起使用
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将C++中的库和头与MinGW一起使用
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 为什么我不能将 rand() 与数组的大小一起使用?
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 不能将复制初始化与隐式转换的多个步骤一起使用
- 将fold表达式与std::一起用于两个元组
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 将 std::allocate_shared 与多态资源分配器一起使用
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- 将 exprtk 与自定义类的对象一起使用
- 将 std::set 与基于键的比较器一起使用
- 将 C++ 类与 Rcpp 一起使用,从 C 或 R 修改它
- 如何将 Eigen::Ref 与 pybind11 一起使用?
- 如何将AERT_Allocate与 std:vector 一起使用
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 在将 new 与指针一起使用时,创建数组的指定长度