Search code examples
c++assertcgal

Assert instead of throw


How can I set the error handling to assert instead of throw? I'd like it to debug break without attaching the debugger.

Currently, I'm patching the source, adding __debugbreak() to assertions_impl.h in precondition_fail().


To be a bit more specific.

There are a handful of global error handling functions.

Their behavior is determined by checking flag from a function get_static_error_behaviour(). Currently, there are two constants to determine the behavior: THROW_EXCEPTION and CONTINUE. I'd like to add to it assert.


This is an example that throws an exception (I found it somewhere, something about CCW order):


#include <CGAL/Simple_cartesian.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/Polygon_set_2.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 CGALPoint;
typedef CGAL::Polygon_2<K> CGALInnerPolygon;
typedef CGAL::Polygon_set_2<K> CGALMultiPolygon;

#include <iostream>

using namespace std;

int main()
{
    CGALInnerPolygon cgalpoly;
    cgalpoly.push_back( CGALPoint( 0, 0 ) );
    cgalpoly.push_back( CGALPoint( 1, 1 ) );
    cgalpoly.push_back( CGALPoint( 1, 0 ) );

    CGALMultiPolygon multipolygon;
    multipolygon.insert( cgalpoly );

    printf( "bye\n" );

    return 0;
}

Something has changed, either the new VS2022 or CGAL, but now it doesn't only print, it also throws an exception and VS breakpoints on the right line.


Solution

  • The behavior on assertion failure can be controlled, as documented here. If I am reading correctly, you want

    #include <CGAL/assertions_behaviour.h>
    int main(){
      CGAL::set_error_behaviour(CGAL::ABORT);
    }
    

    or if that's not quite what you need, you can use CGAL::set_error_handler to provide your own handler.