4.4.  Writing igraph functions with proper error handling

There are some simple rules to keep in order to have functions behaving well in erroneous situations. First, check the arguments of the functions and call IGRAPH_ERROR if they are invalid. Second, call IGRAPH_FINALLY on each dynamically allocated object and call IGRAPH_FINALLY_CLEAN() with the proper argument before returning. Third, use IGRAPH_CHECK on all igraph function calls which can generate errors.

The size of the stack used for this bookkeeping is fixed, and small. If you want to allocate several objects, write a destroy function which can deallocate all of these. See the adjlist.c file in the igraph source for an example.

For some functions these mechanisms are simply not flexible enough. These functions should define their own error handlers and restore the error handler before they return.