2016-05-24 2 views
-1

Ich habe eine QT-Testanwendung (eine App mit einer GUI, die einige Funktionen testet) für mein Arbeitsprojekt gemacht, aber meine Ausnahmen funktionieren überhaupt nicht und ich verstehe nicht warum. Mir fehlt etwas, aber der Code mir vielleicht richtig scheint, hier ist ein Beispiel:Warum funktionieren C++ - try-catch-Blöcke nicht mit g ++ auf ubuntu?

Die Funktion, die die Ausnahme in wirft (mit meinem Test wirft 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; 
} 

Die erste Funktion, die fängt Ausnahme:

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; 
    } 
} 

die Testfunktion:

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; 
    } 
} 

Und schließlich die Schnittstelle, die eine Fehlermeldung zeigen soll:

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"); 
    } 
} 

Jede Idee, warum meine Ausnahme überhaupt nicht eingeklemmt werden? Vielen Dank im Voraus.

edit_1: Ich weiß, ich bin std::invalid_argument nicht fangen, aber das ist, weil es sich um eine Unterklasse von std::logic_error ist nach cplusplus.

+0

Wenn ich 'std :: invalid_argument' nach dem Abfangen von' std :: logic_error' erwische, zeigt QT mir eine Warnung, dass 'std :: invalid_argument' eine Unterklasse von' std :: logic_error' ist, so dass es immer eingefangen wird der Block 'std :: logic_error'. –

+0

Sind Sie sicher, dass es es wirft? – Nim

+0

Ich habe den Debugger verwendet, um es Schritt für Schritt zu überprüfen und ja, es gibt den 'if' Block. Es wirft es dann und nichts passiert. Mein 'normal_estimation_test_form'-Fenster wird deaktiviert (wie es sollte, wenn der Test gestartet wird), aber unter Verwendung von htop (Taskmanager für ubuntu) kann ich sehen, dass meine normale Schätzfunktion nicht läuft (weil es dann 1 Kern bei 100% verwendet) . –

Antwort

0

Ich habe eine Antwort auf das Problem gefunden und es wird hier erklärt: exception handling in Qt. Es sieht nicht nach einem legitimen Weg aus, aber ich weiß nichts Besseres.

Verwandte Themen