Algorithmic Solutions > LEDA > LEDA Guide > Simple, Basic, and Advanced Data Types > Simple Data Types > Error Handling

Error Handling

LEDA tests the preconditions of many operations. If the test of a precondition fails, an error handling routine is called. Users can provide their own error handling function.

Starting with version 4.3 LEDA provides an exception error handler

void exception_error_handler(int num, const char* msg)

This handler uses the C++ exception mechanism and throws an exception of type leda_exception instead of terminating the program.

An object of type leda_exception stores a pair consisting of an error number and an error message.

Remark: The first error handling mechanism is historical. Since most compilers do support exceptions now, you better use the exception error handler.

Example

The following program shows how to use the exception error handler. The constructor circle C(p1,p2,p3) generates a circle through the three points p1, p2, p3. If p1, p2, p3 are collinear C is a straight line passing through p1, p2, p3 in this order and the center of C is undefined. This is the case in our example and, therefore, the assignment p=C.center() causes an exception.

#include <LEDA/geo/circle.h>

int main()
{
  // enable exceptions 
  leda::set_error_handler(leda::exception_error_handler);

  leda::point p1(0,0);leda::point p2(1,1);leda::point p3(2,2);
  leda::circle C(p1,p2,p3);
 
  leda::point p;
 
  try {p =  C.center();}
  catch(leda::leda_exception e) 
  {
    std::cout << std::endl << "An exception occured: " 
         << e.get_msg() << std::endl << std::endl;
    C = leda::circle(leda::point(0,0), leda::point(1,1), leda::point(1,0));
    p =  C.center();
  }
 
  std::cout << "center of circle: " << p << std::endl;
 
  return 0;
}

See also:

Data Types for 2D Geometry

Circles


Manual Page Error Handling




Please send any suggestions, comments or questions to leda@algorithmic-solutions.com
© Copyright 2001-2003, Algorithmic Solutions Software GmbH