BOOST_CHECK_NO_THROW how to get exception message printed - c++

When I test a method using
BOOST_CHECK_NO_THROW( method_to_test() );
and an exception is thrown, it displays that an exception was thrown, but never the exception's message like this:
test.cpp(14): error in "test": incorrect exception my_exception is caught
Is it possible to print the exception message as well, i.e. the string returned by my_exception.what()? my_exception is derived from std::exception and overloads what().

I found myself annoyed by the same problem with BOOST_REQUIRE_NO_THROW. I solved it by simply removing the BOOST_REQUIRE_NO_THROW. This results in output like:
unknown location(0): fatal error in "TestName": std::runtime_error: Exception message
and aborts the test (but goes on with the next text), which is what I wanted. This doesn't help much if you wanted to use BOOST_CHECK_NO_THROW or BOOST_WARN_NO_THROW, though.

I read a bit in the boost headers and redefined BOOST_CHECK_NO_THROW_IMPL in my own header file I use in the project to redefine the boost behavior. Now it looks like this:
#ifndef _CATCH_BOOST_NO_THROW_H_
#define _CATCH_BOOST_NO_THROW_H_
#include <boost/test/unit_test.hpp>
#include <sstream>
#include <string>
#define BOOST_CHECK_NO_THROW_IMPL( S, TL ) \
try { \
S; \
BOOST_CHECK_IMPL( true, "no exceptions thrown by " BOOST_STRINGIZE( S ), TL, CHECK_MSG ); } \
catch( const std::exception & e ) { \
std::stringstream ss; \
ss << std::endl \
<< "-----------------------------------------------" << std::endl \
<< "test case: " << boost::unit_test::framework::current_test_case().p_name << std::endl \
<< std::endl << "exception message: " << e.what() << std::endl; \
BOOST_TEST_MESSAGE(ss.str()); \
BOOST_CHECK_IMPL( false, "exception thrown by " BOOST_STRINGIZE( S ), TL, CHECK_MSG ); \
} \
catch( ... ) { \
std::stringstream ss; \
ss << std::endl \
<< "-----------------------------------------------" << std::endl \
<< "test case: " << boost::unit_test::framework::current_test_case().p_name << std::endl \
<< std::endl << "exception message : <unknown exception>" << std::endl; \
BOOST_TEST_MESSAGE(ss.str()); \
BOOST_CHECK_IMPL( false, "exception thrown by " BOOST_STRINGIZE( S ), TL, CHECK_MSG ); \
} \
/**/
#define BOOST_WARN_NO_THROW( S ) BOOST_CHECK_NO_THROW_IMPL( S, WARN )
#define BOOST_CHECK_NO_THROW( S ) BOOST_CHECK_NO_THROW_IMPL( S, CHECK )
#define BOOST_REQUIRE_NO_THROW( S ) BOOST_CHECK_NO_THROW_IMPL( S, REQUIRE )
#endif // _CATCH_BOOST_NO_THROW_H_
The disadvantages are: It works as long as there are no changes in BOOST_*_NO_THROW
and
the exception message will be printed before it is marked as error in the test output. That looks in the first place a bit pitty, that's why I group the output by writing "---" to the outstream to enhance the reading. But code after BOOST_CHECK_IMPL will never be reached.
The solution above work quite nice for me. Feel free to use to, if you got the same whish =)
(Using CDash for ctest output, don't forget to increase the test output limit, or simple disable the limit: http://web.archiveorange.com/archive/v/5y7PkVuHtkmVcf7jiWol )

Solution 1.
Use a wrapper method that catches the exception, then prints the error message and then re-throws so that BOOST can report it:
void method_to_test(int s)
{
if(s==0)
throw std::runtime_error("My error message");
}
void middle_man(int x)
{
try
{
method_to_test(x);
}catch(std::exception& e)
{
std::stringstream mes;
mes << "Exception thrown: " << e.what();
BOOST_TEST_MESSAGE(mes.str());// BOOST_ERROR(mes.str());
throw;
}
}
Then your test case would read:
BOOST_AUTO_TEST_CASE(case1)
{
BOOST_CHECK_NO_THROW( middle_man(0) );
}
A drawback of this approach is that you will need to use a different middle_man function for every method_to_test.
Solution 2.
Use decorators, see this answer,
to do just what a wrapper from the previous solution would do.
template <class> struct Decorator;
template<class R,class ... Args>
struct Decorator<R(Args ...)>
{
std::function<R(Args ...)> f_;
Decorator(std::function<R(Args ...)> f):
f_{f}
{}
R operator()(Args ... args)
{
try
{
f_(args...);
}catch(std::exception& e)
{
std::stringstream mes;
mes << "Exception thrown: " << e.what();
BOOST_TEST_MESSAGE(mes.str());
throw;
}
}
};
template<class R,class ... Args>
Decorator<R(Args...)> makeDecorator(R (*f)(Args ...))
{
return Decorator<R(Args...)>(std::function<R(Args...)>(f));
}
then your test cases would look like this:
BOOST_AUTO_TEST_CASE(case2)
{
BOOST_CHECK_NO_THROW( makeDecorator(method_to_test)(0) );
BOOST_CHECK_NO_THROW( makeDecorator(another_method_to_test)() );
}

Related

Alternatives to using what() for exception details in boost

I am using the following C++ code from the boost library :
try{
child c(commandString, (std_out & std_err) > pipe_stream);
} catch(process_error &pe){
cout<<pe.what()<<" second line"<<endl;
}
The commandString is a command like ls or any other command. But if a command that does not exist is typed as per the documentation it throws the process_error exception.
I am catching the exception here but not sure if there is a better way to print out the details of the exception or error than pe.what() above?
If you look at the boost reference about boost::process::process_error, you can read this:
struct process_error : public system_error {
};
It merely inherits std::system_error but can then be distinguished in the catch-block from other system errors.
emphasis mine
If you look then at the std::system_error reference, like all other standard exceptions, what() is the way to provide detailed information about the raised error so I would say yes, this is the proper way to print out the exception details.
But since boost::process::process_error does not override the what() function, it would return the same as a std::system_error would.
An alternative interface is using std::error_code.
There are pros and cons:
PRO: it enables you to get more detail about where an error condition arose. (This is a hypothetical difference because it's not specified whether what() may include info beyond an error condition)
CON: it might not get as much detail as in the exception message
CON: since some errors are exceptional, it's harder to express the possible error conditions in the interface: the error conditions get in the way in a way that exceptions do not
in your code you might have created this problem by handling exceptions: now how do decide what value(s) to return
NEUTRAL: you may still have to handle exceptions because exceptions can arise from any related code (e.g. during setup, doing allocations).
NEUTRAL: the code() member of boost::process::process_error is likely to be 100% identical to the error_code that you get
Demo
Contrast the implementations and output below:
Live On Coliru
#include <boost/process.hpp>
#include <iostream>
namespace bp = boost::process;
int using_exceptions(std::string const& commandString) {
try {
bp::pstream pipe_stream;
bp::child c(commandString, (bp::std_out & bp::std_err) > pipe_stream);
c.wait();
return c.exit_code();
} catch (std::exception const& e) {
std::cerr << "Exception: " << e.what() << std::endl;
return -1; // WHAT TO RETURN
}
}
int using_error_code(std::string const& commandString) {
try {
bp::pstream pipe_stream;
std::error_code ec;
bp::child c(commandString, (bp::std_out & bp::std_err) > pipe_stream,
ec);
if (ec) {
std::cerr << "Cannot spawn child process: " << ec.message() << "\n";
return -1; // WHAT TO RETURN?
} else {
c.wait(ec);
std::cerr << "Error: " << ec.message() << "\n";
return c.exit_code();
}
} catch (std::exception const& e) {
std::cerr << "Exception: " << e.what() << std::endl;
return -1; // WHAT TO RETURN
}
}
int main()
{
auto cmd = "/usr/bin/bogus";
std::cout << "Using error_code: " << using_error_code(cmd) << "\n";
std::cout << "Using exceptions: " << using_exceptions(cmd) << "\n";
}
Prints
Using error_code: Cannot spawn child process: No such file or directory
-1
Using exceptions: Exception: execve failed: No such file or directory
-1

How can I support using streams in an exception handling macro in C++?

I have a little error function that looks like this:
template<typename ErrorType>
void throwError(const std::string &file,
const std::string &function,
unsigned int line,
const std::string &msg = "") {
std::ostringstream errMsg;
errMsg << file << ":" << line << ":" << function << ":"
<< "\nError: " << msg << std::endl;
std::cerr << errMsg.str();
throw ErrorType(errMsg.str());
}
Then I have some macros that use the function:
#define INVALID_ARGUMENT_ERROR(msg) throwError<std::invalid_argument>(__FILE__, __func__, __LINE__, msg)
#define LOGIC_ERROR(msg) throwError<std::logic_error>(__FILE__, __func__, __LINE__, msg)
So I can do:
if (condition == bad)
LOGIC_ERROR("you did a bad");
But this is quite invonvenient when I want to add additional information in the error message, like values of numbers for example.
What would be a good way of modifying this function so that it enables me to use a stream instead of a string? So I want do be able to do:
if (condition == bad)
LOGIC_ERROR("you did a bad because condition \"" << condition << " != " << bad);
I've tried changing the std::string string msg to a std::ostringstream which does not work.
If you're willing to change the syntax slightly then a simple helper macro could be used. Given your current function template...
template<typename ErrorType>
void throwError(const std::string &file,
const std::string &function,
unsigned int line,
const std::string &msg = "")
{
std::ostringstream errMsg;
errMsg << file << ":" << line << ":" << function << ":"
<< "\nError: " << msg << std::endl;
std::cerr << errMsg.str();
throw ErrorType(errMsg.str());
}
you can then define...
#define THROW_HELPER(ex_type) \
for (std::stringstream ss; true; throwError<ex_type>(__FILE__, __func__, __LINE__, ss.str())) \
ss
#define INVALID_ARGUMENT_ERROR THROW_HELPER(std::invalid_argument)
#define LOGIC_ERROR THROW_HELPER(std::logic_error)
These can then be used as, e.g...
LOGIC_ERROR << "extra messages go here";
Note that currently two separate std::stringstream instances are created in the process of throwing an exception so the code shown should probably be 'compacted' a bit to prevent that [left as an exercise :-) ].
I propose a different option: libfmt!
#define LOGIC_ERROR(...) \
throwError<std::logic_error>(__FILE__, __func__, __LINE__, fmt::format(__VA_ARGS__))
Usage:
if (condition == bad)
LOGIC_ERROR("you did a bad because condition {} != {}", condition, bad);
The downside of this approach is that fmt::format will throw an exception if the format string is invalid. You could instead check it at compile-time using FMT_STRING() macro:
#define LOGIC_ERROR(string) \
throwError<std::logic_error>(__FILE__, __func__, __LINE__, string)
#define LOGIC_ERROR_P(string, ...) \
throwError<std::logic_error>(__FILE__, __func__, __LINE__, \
fmt::format(FMT_STRING(string), __VA_ARGS__))
Here I've created two versions of LOGIC_ERROR with and without extra parameters. It possible to have a single a single macro dispatch between those two depending on the number of arguments, but that's left as an exercise to the reader.

C++ macro parsing issue

I'm trying to use a macro to queue up a single log line locally in an ostringstream and then dump the entire contents of that ostringstream when the line is over. I want to still use stream insert syntax, however. So I want to turn a log line like this:
std::cerr << "Some error in my function. Code is " << errCode << " exiting" << std::endl;
...into this
SERR("Some error in my function. Code is " << errCode << " exiting);
I've got something simple that works well. That is, until I put it in an if-else statement. Obviously my macro is bad but I am at a loss as to what to do.
Here is a small sample program, hacked up to illustrate the problem.:
#include <iostream>
#include <sstream>
#define SERR(x) { std::ostringstream _s; ; _s << x << std::endl; std::cerr << _s.str(); }
int main()
{
std::cout << "Hello World!\n";
bool val = false;
if (val)
SERR("No error");
else
SERR("Error");
}
The error message I get from the compiler in this sample is:
1>c:\users\joe\source\repos\consoleapplication5\consoleapplication5\consoleapplication5.cpp(15):
error C2181: illegal else without matching if
Any ideas what I've got wrong here?
(Note I'm not in a place where I can use a 3rd Party logging solution now so it's got to be something simple like this. I could just leave all this as normal std::cerr/std::cout/std::clog messages, if I had to but I'd prefer something simple to minimize the chances of interleaving of log messages from my multithreaded app.)
Just try to expand it and see what you get:
#include <iostream>
#include <sstream>
int main()
{
std::cout << "Hello World!\n";
bool val = false;
if (val)
{ std::ostringstream _s; ; _s << "No error" << std::endl; std::cerr << _s.str(); };
else
{ std::ostringstream _s; ; _s << "Error" << std::endl; std::cerr << _s.str(); };
}
Notice how { } block is terminated with ;?
If you need a macro, you should always write it using do { } while (0) like this:
#define SERR(x) \
do { \
std::ostringstream _s; \
_s << (x) << std::endl; \
std::cerr << _s.str(); \
} while (0)
Not only this solves your issue, but also make it mandatory to add ; after the macro. Otherwise people could use it two ways:
SERR("foo") // without ;
...
SERR("bar"); // with ;

Adding error_info to std::exception

I'm integrating boost::exception into existing code. Some of the code now uses BOOST_THROW_EXCEPTION, but some might still throw standard std::exception.
I want to add error_info at an intermediary catch site. According to the docs, if the exception is a boost::exception, I can just do this:
try {
do_something()
}
catch( boost::exception & e ) {
e << boost::errinfo_file_name(file_name);
throw;
}
But that will only add the info to the boost exceptions. I want to add it to the std::exception as well. What's the cleanest way to do that? Here's one way, but it results in some code duplication:
try {
do_something()
}
catch( boost::exception & e ) {
e << boost::errinfo_file_name(file_name);
throw;
}
catch( std::exception & e ) {
throw enable_error_info(e) << boost::errinfo_file_name(file_name);
}
Is there a method that's equivalent to "give me the current exception as a boost exception, or create a boost exception out of it if it isn't one"?
EDIT: boost::enable_error_info() kinda does that but returns a copy of the original exception, which slices off the boost::exception part of the exception I'm catching. Case in point:
int main()
{
try {
try {
BOOST_THROW_EXCEPTION( std::runtime_error( "foo" ) );
}
catch( std::exception & e ) {
std::cerr << e.what() << std::endl; // "foo"
if( const char* const* function = boost::get_error_info<boost::throw_function>(e) ) std::cerr << *function << std::endl; // "int main()"
throw boost::enable_error_info(e) << boost::errinfo_file_name("bar");
}
}
catch( std::exception & e ) {
std::cerr << e.what() << std::endl; // "std::exception"
if( const char* const* function = boost::get_error_info<boost::throw_function>(e) ) std::cerr << *function << std::endl; // NOTHING
}
return 0;
}
EDIT: I tried using boost::current_exception(), and it slices things off too. Basically, any attempt to copy an exception will loose some of the data because of the slicing caused by multiple inheritance. Same reason as why the docs say you should always rethrow using throw instead of throw e. So I really don't want to incur any copying unless it's necessary.
Ideally, I'd like to write the following, where current_exception_as_boost_exception() returns a reference to the current exception if it is already a boost::exception, otherwise returns the result of calling boost::enable_error_info on it.
try {
do_something()
}
catch( std::exception & e ) {
throw current_exception_as_boost_exception() << boost::errinfo_file_name(file_name);
}
Is that what boost::enable_current_exception is for? It's really unclear what its purpose is, and it's not used in any of the tutorials.
Here is a solution that does what I want. But if feels like I'm reinventing something here. Isn't there a built-in way to achieve the same thing?
struct rethrow
{
rethrow()
{
try{ throw; }
// Already a boost::exception
catch( boost::exception& ) {}
// Something else. Make it a boost::exception
catch( ... ) { ptr = boost::current_exception(); }
}
template<class T>
rethrow const& operator<<( const T& t ) const
{
try
{
re();
}
catch( boost::exception& e )
{
e << t;
}
return *this;
}
~rethrow()
{
re();
}
private:
void re() const
{
if( !ptr ) throw;
else boost::rethrow_exception( ptr );
}
boost::exception_ptr ptr;
};
int main()
{
try {
try {
throw std::runtime_error( "foo" ); // or BOOST_THROW_EXCEPTION( std::runtime_error( "foo" ) );
}
catch( std::exception & e ) {
rethrow() << boost::errinfo_file_name("bar");
}
}
catch( std::exception & e ) {
std::cerr << __LINE__ << ": caught " << e.what() << std::endl; // "caught foo"
if( const char* const* function = boost::get_error_info<boost::throw_function>(e) ) std::cerr << __LINE__ << ": throw from " << *function << std::endl; // "throw from int main()" (when using BOOST_THROW_EXCEPTION)
if( std::string const* fileName = boost::get_error_info<boost::errinfo_file_name>(e) ) std::cerr << __LINE__ << ": trying to open " << *fileName << std::endl; // "trying to open bar"
}
return 0;
}

How to make exception code DRY?

I am trying to debug my application by using exception catch-rethrows. My exception handling code is longer than some of the blocks I am debugging, and it's all copy-pasted.
Is there a better way to repeatedly express the below code? I suspect macros are the way to go here, but I usually avoid macros like the plague.
try {
// Code here...
}
catch (std::exception & e)
{
ErrorMsgLog::Log("Error", "std exception caught in " __func__ " " __FILE__ " " __LINE__, e.what());
throw e;
}
catch (Exception & e)
{
ErrorMsgLog::Log("Error", "Builder exception caught in " __func__ " " __FILE__ " " __LINE__, e.Message);
throw e;
}
catch (...)
{
ErrorMsgLog::Log("Error", "Unknown exception caught in " __func__ " " __FILE__ " " __LINE__);
throw std::runtime_error ("Unknown Exception in " __func__ " " __FILE__ " " __LINE__);
}
The best way to implement this is probably using macros. The macro definition is a little bit ugly, but calling the macro will be pretty easy, and you will not need to re-organize your code. Here is a sample that shows how you could implement it:
#define RUN_SAFE(code) try {\
code\
}\
catch (std::exception & e)\
{\
ErrorMsgLog::Log("Error");\
throw e;\
}\
catch (Exception & e)\
{\
ErrorMsgLog::Log("Error");\
throw e;\
}\
catch (...)\
{\
ErrorMsgLog::Log("Error");\
throw std::exception();\
}\
int main(){
RUN_SAFE(
cout << "Hello World\n";
)
}
If you are really adamant about not using macros, you could use the approach suggested by #juanchopanza and use a higher-order function for the check that takes the code as a parameter. This approach will probably require you to refactor your code a bit though. Here is how you could implement it:
void helloWorld(){
cout << "Hello World\n";
}
void runSafe(void (*func)()){
try {
func();
}
catch (std::exception & e)
{
ErrorMsgLog::Log("Error");
throw e;
}
catch (Exception & e)
{
ErrorMsgLog::Log("Error");
throw e;
}
catch (...)
{
ErrorMsgLog::Log("Error");
throw std::exception();
}
}
int main(){
runSafe(helloWorld);
}