[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

Error Reporting |

Exceptions and assertions provided by VIGRA

#include <vigra/error.hxx>

VIGRA defines the following exception classes:

        namespace vigra {
            class ContractViolation : public std::exception;
            class PreconditionViolation : public ContractViolation;
            class PostconditionViolation : public ContractViolation;
            class InvariantViolation : public ContractViolation;
        }

The following associated macros throw the corresponding exception if their PREDICATE evaluates to '`false`':

        vigra_precondition(PREDICATE, MESSAGE);
        vigra_postcondition(PREDICATE, MESSAGE);
        vigra_invariant(PREDICATE, MESSAGE);

The MESSAGE is passed to the exception and can be retrieved via the overloaded member function '`exception.what()`'. If the compiler flag '`NDEBUG`' is not defined, the file name and line number of the error are automatically included in the message. The macro

        vigra_assert(PREDICATE, MESSAGE);

is identical to `vigra_precondition()` except that it is completely removed when '`NDEBUG`' is defined. This is useful for test that are only needed during debugging, such as array index bound checking. The following macro

        vigra_fail(MESSAGE);

unconditionally throws a '`std::runtime_error`' constructed from the message (along with file name and line number, if NDEBUG is not set).

Usage:

Include-File: <vigra/error.hxx>

Namespace: vigra (except for the macros, of course)

        int main(int argc, char ** argv)
        {
            try
            {
                const char* input_file_name = argv[1];

                // read input image
                vigra::ImageImportInfo info(input_file_name);

                // fail if input image is not grayscale
                vigra_precondition(info.isGrayscale(), "Input image must be grayscale");

                ...// process image
            }
            catch (std::exception & e)
            {
                std::cerr << e.what() << std::endl; // print message
                return 1;
            }

            return 0;
        }

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany | html generated using doxygen and Python vigra 1.9.0 (Tue Nov 6 2012)