How to Add Message to Assert in C++ Last Updated : 27 May, 2024 Comments Improve Suggest changes Like Article Like Report In C++, assert is a debugging tool that allows the users to test assumptions in their code. The standard assert macro provided by <cassert> checks the condition and if the condition is evaluated as false, the assert statement terminates the program and prints an error message on the console along with the source code file name and the line number. In this article, we will learn how to add a message to assert in C++. Add Message to Assert in C++By default, the error message printed by the assert statement is not informative and consists of the source code file name and the line number. The following message is printed by default by the assert: Assertion failed: condition, file file_name.cpp, line number ...The default error returned by the assert does not give much context about why the assert got executed or what the expected behavior was. Adding a message to the assert statement can provide more useful debugging information to the user and make it easier to identify and fix bugs. To add a custom message to an assert statement in C++, the following syntax can be followed: Syntax#define ASSERT(condition, message) \ do { \ assert(condition && message); \ } while (0)where: ASSERT: is a macro that takes two arguments condition and message.Condition: is defined as the condition the user wants to check for.Message: is the custom message string that the user wants to return on the console when the assertion is executed.Note: Inside the macro, built in assert is used that concatenates the condition and the message string using the logical and (&&) operator and the stringizing(#) operator. C++ Program to Add Message to Assert The following program illustrates how we can add a custom message to assert in C++: C++ // C++ Program to Add Message to Assert #include<iostream> #include <cassert> #include <cstdio> using namespace std; //Define the macro for assert #define ASSERT(condition, message) \ do { \ assert(condition && #message); \ } while (0) int main() { int x = 20; int y = 10; // Adding condition and message to the assert ASSERT(x + y == 15, "x + y should be 15"); cout<<"Program Executed Successfully"<<endl; return 0; } Output a.out: main.cpp:16: int main(): Assertion `x + y == 15 && "\"x + y should be 15\""' failed.Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Add Message to Assert in C++ susobhanakhuli Follow Improve Article Tags : C++ Programs C++ C++ Errors CPP Examples misc-cpp +1 More Practice Tags : CPP Similar Reads How to Get Error Message When ifstream Open Fails in C++? In C++, the std::ifstream class is used to open a file for input operations. It associates an input stream to the file. However, due to some reasons, it may be unable to open the requested file. In this article, we will learn how to show an error message when the ifstream class fails to open the fil 2 min read How to Throw and Catch Exceptions in C++? In C++, exception handling is a mechanism that allows us to handle runtime errors and exceptions are unusual conditions that occur at runtime. In this article, we will learn how to throw and catch exceptions in C++. Throw and Catch Exceptions in C++In C++ exceptions can be "thrown" when an error occ 2 min read How to Throw a Custom Exception in C++? In C++, exception handling is done by throwing an exception in a try block and catching it in the catch block. We generally throw the built-in exceptions provided in the <exception> header but we can also create our own custom exceptions.In this article, we will discuss how to throw a custom e 2 min read How to Throw an Exception in C++? In C++, exception handling is a mechanism that allows us to handle runtime errors and exceptions are objects that represent an error that occurs during the execution of a program. In this article, we will learn how to throw an exception in C++. Throw a C++ ExceptionThrowing an exception means sendin 2 min read How to Catch a Specific Exception in C++? In C++, exceptions are runtime anomalies or abnormal conditions that a program encounters during its execution. The process of handling these exceptions is called exception handling. In this article, we will learn how we can catch specific exceptions in C++. Catch a Specific Exception in C++ In C++, 2 min read How to Use cin.fail() Method in C++? In C++, the cin.fail() method is a part of <iostream> library that is used to check whether the previous input operation has succeeded or not by validating the user input. In this article, we will learn how to use cin.fail() method in C++. Example: Input: Enter an integer: aOutput: Invalid Inp 2 min read How to Handle SIGABRT Signal in C++ In C++, SIGABRT short for Signal Abort is a signal used by the abort function to indicate an abnormal program termination. This is commonly used to signal critical errors where continuing the execution of the program is not possible. In this article, we will learn how to handle SIGABRT Signal in C++ 3 min read How to use errno in C++? In C++ errno is a preprocessor macro that is used for error indication and reporting errors that occur during a function call. It contains error codes so if a call to a function fails somehow then the errno is set to a value that corresponds to the error. errno is defined in the header file <cerr 4 min read How to Catch All Exceptions in C++? In C++, exceptions are objects that indicate you have an error in your program. They are handled by the try-catch block in C++. In this article, we will learn how to catch all the exceptions in C++. Catching All Exceptions in C++To catch all kinds of exceptions in our catch block in C++, we can defi 2 min read How Should I Use FormatMessage() in C++? In C++, the FormatMessage() is a function used for formatting error messages returned by the system that is particularly useful for retrieving system error messages and converting them into a user-readable format. In this article, we will learn how we should use the FormatMessage function properly i 3 min read Like