How to Add Message to Assert in C++
Last Updated :
27 May, 2024
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)
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 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