Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom error message of re-thrown exception not printed by what()

I'm writing a set of custom exceptions that extend std::exception. In some code, when an exception is caught, I just re-throw it up the chain until the driver main function call catches and prints the result. However, ultimately all that gets printed is "std::exception". This doesn't appear the be the scope issue I dealt with previously.

Why are my exception messages not printing?

My exception code:

// General exception class
struct MyException : public std::exception
{
    std::string _msg;

    MyException(const std::string &exception_name) : _msg(exception_name) {}

    void setMessage(const std::string &message)
    {
        _msg += ": " + message + "\n";
    }

    void setLocation(const char * func, const char * file, const int line)
    {
        _msg += "  In function " + std::string(func) + "(" + file + ":" + std::to_string(line) + ")";
    }

    const char * what() const throw()
    {
        return _msg.c_str();
    }
};

// Specializations of the MyException
struct FileNotFoundException : public MyException
{
    FileNotFoundException() : MyException("FileNotFoundException") {}
};

struct IOException : public MyException
{
    IOException() : MyException("IOException") {}
};
struct DBException : public MyException
{
    DBException() : MyException("DBException") {}
};

All of my exception throws are wrapped in this macro

#define EXCEPTION_THROWER(ET, message)              \
    {                           \
        ET e;                       \
        e.setMessage(message);              \
        e.setLocation(__func__, __FILE__, __LINE__);    \
        throw e;                    \
    }

and called as

EXCEPTION_THROWER(DBException, "Blah blah database exception")

The intermediate try/catch blocks look like this:

try
{
    // Call a function that throws an exception
}
catch(const std::exception &e)
{
    throw e; // Forward any exceptions
}

and the driver code is all in one try block with a catch (const std::exception &e) block.

like image 340
marcman Avatar asked Jun 27 '17 14:06

marcman


1 Answers

throw e; is carrying out a bucket load of object slicing as it is essentially slicing whatever e was to a std::exception (and _msg will be lost).

Use throw; to rethrow the caught exception by reference.

like image 117
Bathsheba Avatar answered Sep 27 '22 22:09

Bathsheba