1

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.

Zohar Levi
  • 614
  • 6
  • 16
  • 3
    You should never use `assert` instead of `throw` for error handling. `assert` must only be used for situations that really **are not possible**. – akaAbdullahMateen Jul 16 '22 at 23:59
  • Have a look [here](https://softwareengineering.stackexchange.com/questions/15515/when-to-use-assertions-and-when-to-use-exceptions) and [here](https://stackoverflow.com/questions/409794/exception-vs-assert) – Irelia Jul 17 '22 at 00:05
  • I don't expect cgal lib to change its behavior from throw to assert. However, I expect a flag that I can set that would enable me to switch and use the latter conveniently. – Zohar Levi Jul 17 '22 at 00:18
  • If cgal doesn't provide a flag, then you're out of luck. Since assert and exceptions serve entirely different purposes though, it'd be surprising if any such flag existed. – Stephen Newell Jul 17 '22 at 00:33
  • @ZoharLevi `if (some_condition) throw SomeException;` -- What would the `assert()` version of that `throw` statement look like? There is trouble trying to "convert" the `throw` to `assert` by hand (deciding what the assert() will contain), let alone find some sort of macro to do the job. – PaulMcKenzie Jul 17 '22 at 01:01
  • I added details. – Zohar Levi Jul 17 '22 at 05:45

1 Answers1

3

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.

Marc Glisse
  • 7,550
  • 2
  • 30
  • 53