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 catch
es 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With