| Home | Libraries | People | FAQ | More

## Class assertion_failure

boost::contract::assertion_failure — Exception typically used to report a contract assertion failure.

## Synopsis

    // In header: <boost/contract/core/exception.hpp>

    class assertion_failure :
      public std::exception, public boost::contract::exception
    {
    public:
      // construct/copy/destruct
      explicit assertion_failure(char const * = "", unsigned long = 0,
                                 char const * = "");
      explicit assertion_failure(char const *);
      ~assertion_failure();

      // public member functions
      virtual char const * what() const;
      char const * file() const;
      unsigned long line() const;
      char const * code() const;
    };

## Description

This exception is thrown by code expanded by `BOOST_CONTRACT_ASSERT` (but it can also be thrown by user code programmed manually without that macro). This exception is typically used to report contract assertion failures because it contains detailed information about the file name, line number, and source code of the asserted condition (so it can be used by this library to provide detailed error messages). However, any other exception can be used to report a contract assertion failure (including user-defined exceptions).

This library will call the appropriate contract failure handler function ( `boost::contract::precondition_failure`, etc.) when this or any other exception is thrown while checking contracts (by default, these failure handler functions print an error message to `std::cerr` and terminate the program, but they can be customized to take any other action).

See Also:

Throw on Failure, No Macros

### `assertion_failure` public construct/copy/destruct

1.     explicit assertion_failure(char const * file = "", unsigned long line = 0,
                                  char const * code = "");

   Construct this object with file name, line number, and source code text of an assertion condition (all optional).

   This constructor can also be used to specify no information (default constructor), or to specify only file name and line number but not source code text (because of the parameter default values).

   Parameters: |
   `code` | Text listing the source code of the assertion condition.
   `file` | Name of the file containing the assertion (usually set using `__FILE__`).
   `line` | Number of the line containing the assertion (usually set using `__LINE__`).

2.     explicit assertion_failure(char const * code);

   Construct this object only with the source code text of the assertion condition.

   Parameters: |
   `code` | Text listing the source code of the assertion condition.

3.     ~assertion_failure();

   Destruct this object.

   Throws: This is declared `noexcept` (or `throw()` before C++11).

### `assertion_failure` public member functions

1.     virtual char const * what() const;

   String describing the failed assertion.

   Throws: This is declared `noexcept` (or `throw()` before C++11).

   Returns: | A string formatted similarly to the following: `assertion "`code()`" failed: file "`file()`", line `line()``. File, line, and code will be omitted from this string if they were not specified when constructing this object.

2.     char const * file() const;

   Name of the file containing the assertion.

   Returns: | File name as specified at construction (or `""` if no file was specified).

3.     unsigned long line() const;

   Number of the line containing the assertion.

   Returns: | Line number as specified at construction (or `0` if no line number was specified).

4.     char const * code() const;

   Text listing the source code of the assertion condition.

   Returns: | Assertion condition source code as specified at construction (or `""` if no source code text was specified).