.. index:: py::builtins::assert .. _doxycast_pypp_namespacepy_1_1builtins_1ac1925647ee1f0e5cb405d180e4587dbf: .. _py-builtins-assert: *py::builtins::*\ assert ======================== *Defined in* ```` .. code-block:: cpp void assert(bool expression); // 1 void assert(bool expression, std::string message); // 2 1. Assert an expression and throws on failure. 2. Assert an expression and throws on failure with given message. ``py::builtins::assert`` asserts whether is given value is ``true`` (usually returned from an expression) and raises :ref:`py-builtins-AssertionError` on failure. Parameters ---------- ``expression``: The expression to assert. ``message``: The message of exception message. Complexity ---------- Constant. Example ------- .. code-block:: cpp #include #include int main() { py::assert(true); try { py::assert(false); } catch (py::AssertionError&) { py::print("assert failed: py::assert(false)"); } } **Output**: .. code-block:: text assert failed: py::assert(false)