I am trying to throw a std::exception from managed code so that it is caught in unmanaged code. Where I'm struggling is passing a string (describing the exception) so that the (re-)caught exception can be examined using the what() method ...
#pragma managed
static std::string InvokeMethod() {
try {
//...
}
catch (Exception^ ex) {
std::string myExMsg = msclr::interop::marshal_as<std::string>(ex->ToString());
throw std::exception(myExMsg);
}
}
#pragma unmanaged
void Execute() {
try {
myMethod = InvokeMethod();
}
catch (std::exception ex) {
SetError(ex.what());
}
}
This doesn't compile with "no instance of constructor "stdext:exceptipon::exception" matches the argument list argument types are (std::string)" BUT if I 'hard code' a string into std::exception like this ...
throw std::exception("An error has occurred");
... then that string gets passed along and returned by ex.what(). I also tried ...
throw std::runtime_error(myExMsg);
... but ex.what() just returns a string ending with '\x7F' (in case that's a clue).
It looks to me that std::exception is expecting some other type. But what type? What does 'myExMsg' need to be so that ex.what() returns the same string (that can be used in the SetError method)?
Following suggestion made by #Joe, I inherit from std::exception ...
class InvokeException : public std::exception {
public:
InvokeException(std::string const& message) : msg_(message) { }
virtual char const* what() const noexcept { return msg_.c_str(); }
private:
std::string msg_;
};
... and then ...
const std::string myExMsg = msclr::interop::marshal_as<std::string>(ex->Message);
throw InvokeException(myExMsg);
I'm using Catch2 to write my unit tests.
One thing I want to do is make sure that I'm catching the correct exception. I throw the same exceptions in many circumstances, so just knowing that I'm catching an std::logic_error doesn't prove that a specific instance of the exception was indeed caught.
Catch2 provides the REQUIRE_THROWS_MATCHES() macro for that purpose.
Here is an example of how I use it with the Equals matcher:
CATCH_REQUIRE_THROWS_MATCHES(
std::make_shared<advgetopt::getopt>(
options_environment
, sub_argc
, sub_argv)
, advgetopt::getopt_exception_logic
, Catch::Matchers::Equals(
"section \"invalid::name\" includes a section separator (::) in \""
+ options_filename
+ "\". We only support one level."));
Only that does not compile unless I have a cast operator in my exceptions. In this case, that's easy enough since I have my own exception. But I'm wondering why the author of Catch2 thought of using a cast to std::string instead of using the what() function.
Here is my current base class exception definition:
class logic_exception_t
: public std::logic_error
, public exception_base_t
{
public:
explicit logic_exception_t( std::string const & what, int const stack_trace_depth = STACK_TRACE_DEPTH );
explicit logic_exception_t( char const * what, int const stack_trace_depth = STACK_TRACE_DEPTH );
virtual ~logic_exception_t() override {}
virtual char const * what() const throw() override;
operator std::string () const;
};
Here is the operator std::string () const function:
logic_exception_t::operator std::string () const
{
return what();
}
Is there another way to satisfy the Catch2 requirement and allow for a transformation of an exception to an std::string without having to create a cast operator? I just don't like having a cast which could cause other problems down the road.
Note: I tried to make the cast explicit and Catch2 doesn't like it either. It just passes the exception to a function which expects an std::string.
I ran into the same problem today; there is a half-way solution in the form of a Message matcher, documented in the Catch2 documentation.
REQUIRE_THROWS_MATCHES(fn(), SomeException, Message("Complete exception emssage"));
However, I would like to have a Contains like functionality. Given the Message implementation, it is trivial to implement it:
#include <catch2/matchers/catch_matchers.hpp>
#include <exception>
#include <string>
#include <string.h>
class ExceptionMessageContainsMatcher final : public MatcherBase<std::exception>
{
std::string m_messagePart;
public:
ExceptionMessageContainsMatcher(const std::string &messagePart)
: m_messagePart{messagePart}
{}
bool match(const std::exception &e) const {
return ::strstr(e.what(), m_messagePart.data()) != nullptr;
}
std::string describe() const {
return "exception message does not contain \"" + m_messagePart + '"';
}
};
ExceptionMessageContainsMatcher MessageContains(const std::string &message) {
return {message};
}
This allows you to write:
REQUIRE_THROWS_MATCHES(fn(), SomeException, MessageContains("part of the message"));
You can actually define your own watcher, so I decided to write a watcher that would take an exception for its match() function. This works without the casting to std::string!
namespace Catch
{
namespace Matchers
{
class ExceptionWatcher
: public MatcherBase<std::exception>
{
public:
ExceptionWatcher(std::string const & expected_message)
: m_expected_message(expected_message)
{
}
/** \brief Check whether we got a match.
*
* This function compares the expected string with the actual exception
* what() output.
*/
bool match(std::exception const & e) const override
{
return e.what() == m_expected_message;
}
/** \brief Describe this matcher.
*
* This function produces a string describing what this matcher does.
*
* \return The description of this matcher.
*/
virtual std::string describe() const override
{
return "compare the exception what() message with \""
+ m_expected_message
+ "\".";
}
private:
std::string m_expected_message = std::string();
};
inline ExceptionWatcher ExceptionMessage(std::string const & expeted_message)
{
return ExceptionWatcher(expeted_message);
}
}
// Matchers namespace
}
// Catch namespace
class GAGenome {
virtual void method(){};
};
template <class T>
class GAArray {
};
template <class T>
class GA1DArrayGenome : public GAArray<T>, public GAGenome {
};
int main() {
GA1DArrayGenome<float> genome;
const GAGenome & reference = genome;
auto cast = dynamic_cast<const GA1DArrayGenome<int> &>(reference);
}
This obviously wrong program (since template parameter is different) crashes with
terminate called after throwing an instance of 'std::bad_cast'
what(): std::bad_cast
Aborted (core dumped)
Is there a way how to get precise diagnostics of what went wrong, beyond the runtime error message? Something, that can point out the int/float mistake to me? I am looking for a descriptive error message like
const GA1DArrayGenome<float> & cannot be cast to const GA1DArrayGenome<int> &
Even better, since C++ types can get hairy at times, the tool could notice the precise discrepancy in the template parameter.
You can also abandon direct uses of dynamic_cast and wrap it in your own template machinery:
#include <sstream>
class my_bad_cast: public std::bad_cast {
public:
my_bad_cast(char const* s, char const* d): _source(s), _destination(d) {
#ifdef WITH_BETTER_WHAT
try {
std::ostringstream oss;
oss << "Could not cast '" << _source
<< "' into '" << _destination << "'";
_what = oss.str();
} catch (...) {
_what.clear();
}
#endif
}
char const* source() const { return _source; }
char const* destination() const { return _destination; }
#ifdef WITH_BETTER_WHAT
virtual char const* what() const noexcept {
return not _what.empty() ? _what.c_str() : std::bad_cast::what();
}
#endif
private:
char const* _source;
char const* _destination;
#ifdef WITH_BETTER_WHAT
std::string _what;
#endif
// you can even add a stack trace
};
template <typename D, typename S>
D my_dynamic_cast(S&& s) {
try {
return dynamic_cast<D>(std::forward<S>(s));
} catch(std::bad_cast const&) {
throw my_bad_cast(typeid(S).name(), typeid(D).name());
}
}
You can load your program (compiled with debug information, e.g. -g in gcc and glang) in gbd, tell gdb to catch exceptions with catch throw and then look at the call stack to see exactly where the exception was thrown.
std::bad_cast is thrown when a dynamic_cast fails at runtime.
I'd like to implement an Exception class in C++ that mimics the one from .NET framework (and Java has something similar too), for the following purposes:
Exception chaining: I'd like to implement the concept of "exception translation", when exceptions caught at higher levels wrap and "translate" the lower level exceptions, also preserving these lower-level exceptions somehow (in the InnerException member, in this case). For this, there should be some mechanism to store inner exceptions along with each exception thrown at the upper level. InnerException member provides this in the implementation below.
Exception inheritance: there should be possible to derive IoException from Exception, and SerialPortException from IoException, for example. While this seems trivial, there should be ability to identify the type of caught exceptions dynamically (e.g. for logging purposes, or to display to user), preferably without the overhead of RTTI and typeid.
This is the sample exception handling logic I'd like to make possible:
try
{
try
{
try
{
throw ThirdException(L"this should be ThirdException");
}
catch(Exception &ex)
{
throw SubException(L"this should be SubException", ex);
}
}
catch(Exception &ex)
{
throw SubException(L"this should be SubException again", ex);
}
}
catch(Exception &ex)
{
throw Exception(L"and this should be Exception", ex);
}
and when catching the "outer-most" exception in the upmost layer I'd like to be able to parse and format whole exception chain through the InnerException member, to display something like this:
I've come up with the following implementation so far:
Small note: CString is Microsoft-specific string class (just for the people not familiar with Visual C++ stuff).
class Exception
{
protected:
Exception(const Exception&) {};
Exception& operator= (const Exception&) {};
public:
Exception(const CString &message) : InnerException(0), Message(message) {}
Exception(const CString &message, const Exception &innerException) : InnerException(innerException.Clone()), Message(message) {}
virtual CString GetExceptionName() const { return L"Exception"; }
virtual Exception *Clone() const
{
Exception *ex = new Exception(this->Message);
ex->InnerException = this->InnerException ? this->InnerException->Clone() : 0;
return ex;
}
public:
virtual ~Exception() { if (InnerException) delete InnerException; }
CString Message;
const Exception *InnerException;
};
Now what do we have here. Copy constructor and assignment operator are made protected to prevent copying. Each object will "own" its inner exception object (and delete it in destructor), so default shallow-copying would be unacceptable. Then we have two pretty standard-looking constructors and virtual destructor that deletes the InnerException object. Clone() virtual method is responsible for deep-copying the objects, primarily for storing the inner exception object (see the second constructor). And finally GetExceptionName() virtual method provides the cheap alternative to RTTI for identification of exception class names (I don't think this looks cool but I couldn't come up with better solution; for comparison: in .NET one could simply use someException.GetType().Name).
Now this does the job. But... I don't like this solution for one particular reason: the amount of coding needed for each derived class. Consider I have to derive SubException class, which provides absolutely zero additions to the base class functionality, it just provides the custom name ("SubException", which might be "IoException", "ProjectException", ...) to differentiate it for its usage scenario. I have to provide almost same amount of code for each of such exception class. Here it is:
class SubException : public Exception
{
protected:
SubException(const SubException& source) : Exception(source) {};
SubException& operator= (const SubException&) {};
public:
SubException(const CString &message) : Exception(message) {};
SubException(const CString &message, const Exception &innerException) : Exception(message, innerException) {};
virtual CString GetExceptionName() const { return L"SubException"; }
virtual Exception *Clone() const
{
SubException *ex = new SubException(this->Message);
ex->InnerException = this->InnerException ? this->InnerException->Clone() : 0;
return ex;
}
};
I don't like the fact that I have to provide protected copy constructor and assignment operator each time, I don't like the fact that I have to clone the Clone method each time, duplicating even the code of copying the base members (InnerException...), simply... I don't think this is the elegant solution. But I was unable to think of better one. Do you have any ideas how to implement this concept "properly"? Or maybe this is the best implementation of this concept that is possible in C++? Or maybe I'm doing this completely wrong?
P.S.: I know there exist some mechanisms in C++11 (also in Boost) for this purpose (exception chaining) with some new exception classes, but I'm primarily interested in custom, "old-C++-compatible" ways. But it would be good, in addition, if someone could provide any code in C++11 that accomplishes the same.
C++11 already has nested_exception. There was a talk about exceptions in C++03 and C++11 at Boostcon/C++Next 2012. Videos are on youtube:
http://www.youtube.com/watch?v=N9bR0ztmmEQ&feature=plcp
http://www.youtube.com/watch?v=UiZfODgB-Oc&feature=plcp
There is a lot of extra code, but the good thing is it's really EASY extra code that doesn't change at all from class to class, so it's possible to preprocessor macro it.
#define SUB_EXCEPTION(ClassName, BaseName) \
class ClassName : public BaseName\
{\
protected:\
\
ClassName(const ClassName& source) : BaseName(source) {};\
ClassName& operator= (const ClassName&) {};\
\
public:\
\
ClassName(const CString &message) : BaseName(message) {};\
ClassName(const CString &message, const BaseName &innerException) : BaseName(message, innerException) {};\
\
virtual CString GetExceptionName() const { return L"ClassName"; }\
\
virtual BaseName *Clone() const\
{\
ClassName *ex = new ClassName(this->Message);\
ex->InnerException = this->InnerException ? this->InnerException->Clone() : 0;\
return ex;\
}\
};
Then you can define various utility exceptions by just doing:
SUB_EXCEPTION(IoException, Exception);
SUB_EXCEPTION(SerialPortException, IoException);
Please don't follow boost::exception approach. Boost::exception is for different use case - in particular it's usefull when you want to collect precise exception context scatered over call stack. Consider the following example:
#include "TSTException.hpp"
struct DerivedException: TST::Exception {};
int main() try
{
try
{
try
{
try
{
throw std::runtime_error("initial exception");
}
catch(...)
{
throw TST::Exception("chaining without context info");
}
}
catch(...)
{
TST_THROW("hello world" << '!');
}
}
catch(...)
{
TST_THROW_EX(DerivedException, "another exception");
}
}
catch(const TST::Exception& ex)
{
cout << "diagnostics():\n" << ex;
}
catch(const std::exception& ex)
{
cout << "what(): " << ex.what() << endl;
}
The "exception chaining" solution as I understand it should produce output similar to this:
$ ./test
diagnostics():
Exception: another exception raised from [function: int main() at main.cpp:220]
Exception: hello world! raised from [function: int main() at main.cpp:215]
Exception: chaining without context info raised from [function: unknown_function at unknown_file:0]
Exception: initial exception
As you see there are exceptions chained to each other and diagnostic output contains all exceptions with context information and optional stack trace (not shown here, because it's compiler/platform dependent).
"Exception chaining" can be naturally achieved using new C++11 error handling features (std::current_exception or std::nested_exception). Here is implementation of TSTException.hpp (please bear with more source code):
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <exception>
#include <vector>
#include <string>
#include <memory>
#include <boost/current_function.hpp>
#include <boost/foreach.hpp>
using namespace std;
namespace TST
{
class Exception: virtual public std::exception
{
public:
class Context
{
public:
Context():
file_("unknown_file"),
line_(0),
function_("unknown_function")
{}
Context(const char* file, int line, const char* function):
file_(file? file: "unknown_file"),
line_(line),
function_(function? function: "unknown_function")
{}
const char* file() const { return file_; }
int line() const { return line_; }
const char* function() const { return function_; }
private:
const char* file_;
int line_;
const char* function_;
};
typedef std::vector<std::string> Stacktrace;
//...
Exception()
{
initStacktraceAndNestedException();
}
explicit Exception(const std::string& message, const Context&& context = Context()):
message_(message),
context_(context)
{
message.c_str();
initStacktraceAndNestedException();
}
~Exception() throw() {}
//...
void setContext(const Context& context) { context_ = context; }
void setMessage(const std::string& message) { (message_ = message).c_str(); }
const char* what() const throw () { return message_.c_str(); }
void diagnostics(std::ostream& os) const;
protected:
const Context& context() const { return context_; }
const std::exception_ptr& nested() const { return nested_; }
const std::shared_ptr<Stacktrace>& stacktrace() const { return stacktrace_; }
const std::string& message() const { return message_; }
private:
void initStacktraceAndNestedException();
void printStacktrace(std::ostream& os) const;
std::string message_;
Context context_;
std::shared_ptr<Stacktrace> stacktrace_;
std::exception_ptr nested_;
};
std::ostream& operator<<(std::ostream& os, const Exception& ex)
{
ex.diagnostics(os);
return os;
}
std::ostream& operator<<(std::ostream& os, const Exception::Context& context)
{
return os << "[function: " << context.function()
<< " at " << context.file() << ':' << context.line() << ']';
}
void Exception::diagnostics(std::ostream& os) const
{
os << "Exception: " << what() << " raised from " << context_ << '\n';
if (const bool haveNestedException = nested_ != std::exception_ptr())
{
try
{
std::rethrow_exception(nested_);
}
catch(const TST::Exception& ex)
{
if(stacktrace_ && !ex.stacktrace())//if nested exception doesn't have stacktrace then we print what we have here
printStacktrace(os);
os << ex;
}
catch(const std::exception& ex)
{
if(stacktrace_)
printStacktrace(os);
os << "Exception: " << ex.what() << '\n';
}
catch(...)
{
if(stacktrace_)
printStacktrace(os);
os << "Unknown exception\n";
}
}
else if(stacktrace_)
{
printStacktrace(os);
}
}
void Exception::printStacktrace(std::ostream& os) const
{
if(!stacktrace_)
{
os << "No stack trace\n";
return;
}
os << "Stack trace:";
BOOST_FOREACH(const auto& frame, *stacktrace_)
{
os << '\n' << frame;
}
os << '\n';
}
void Exception::initStacktraceAndNestedException()
{
nested_ = std::current_exception();
if(const bool haveNestedException = nested_ != std::exception_ptr())
{
try
{
throw;
}
catch(const TST::Exception& ex)
{
if(ex.stacktrace())
{
stacktrace_ = ex.stacktrace();
return;
}
}
catch(...) {}
}
/*TODO: setStacktrace(...); */
}
}//namespace TST
#ifdef TST_THROW_EX_WITH_CONTEXT
#error "TST_THROW_EX_WITH_CONTEXT is already defined. Consider changing its name"
#endif /*TST_THROW_EX_WITH_CONTEXT*/
#define TST_THROW_EX_WITH_CONTEXT( \
CTX_FILE, CTX_LINE, CTX_FUNCTION, EXCEPTION, MESSAGE) \
do \
{ \
EXCEPTION newEx; \
{ \
std::ostringstream strm; \
strm << MESSAGE; \
newEx.setMessage(strm.str()); \
} \
newEx.setContext( \
TST::Exception::Context( \
CTX_FILE, CTX_LINE, CTX_FUNCTION)); \
throw newEx; \
} \
while(0)
#ifdef TST_THROW_EX
#error "TST_THROW_EX is already defined. Consider changing its name"
#endif /*TST_THROW_EX*/
#define TST_THROW_EX(EXCEPTION, MESSAGE) \
TST_THROW_EX_WITH_CONTEXT(__FILE__, __LINE__, BOOST_CURRENT_FUNCTION, EXCEPTION, MESSAGE)
#ifdef TST_THROW
#error "TST_THROW is already defined. Consider changing its name"
#endif /*TST_THROW*/
#define TST_THROW(MESSAGE) \
TST_THROW_EX(TST::Exception, MESSAGE)
I use compiler with partial C++11 support (gcc 4.4.7) so you can see some old style peaces of code here. Just for reference you can use the following compilation parameters to build this example (-rdynamic is for stack trace):
g++ main.cpp TSTException.hpp -rdynamic -o test -std=c++0x
Few years ago I wrote this: Unchaining Chained Exceptions in C++
Basically, the exceptions are not nested inside each other, because it would be difficult to catch the original one, but another mechanism keeps track of all the functions visited by the exception while it travels to its catch point.
A revisited version of that can be found in the library Imebra on Bitbucket, here and here.
Now I would rewrite that with some improvements (e.g. use local thread storage to keep the stack trace).
Using this approach allows you to catch the original exception that was thrown, but to still have the stack trace and possibly other information added by the functions visited by the exception while it travels back to the catch statement.
Note: I can't use anything that is default.
I am trying to make a very simple exception handling routine or at the very least make something that looks the part. I don't want to do very much, just throw an exception and print an error message.
in .h
class MyException {
protected: string message;
public:
MyException (string mes) {
this->message = mes;
}
MyException (); // is this necessary ? does it do anything ?
string getMessage() const {
return this->message;
}
};
What I'd want is to have a "PersonException" and "ActivityException". Might use a template but not sure if that would work out.
class PersonException:public MyException {
public:
PersonException (string message):MyException(message) {
}
};
class PersonValidator {
public:
PersonValidator (Person p) throw (PersonException);
};
in .cpp
void PersonValidator::PersonValidator(Person p) throw (PersonException) {
if (p.getPhone < 0) {
throw PersonException ("Person Number is invalid");
}
What here is wrong or cumbersome, how could it be done better ? and where do I actually print the error message ?
1) The default constructor is not necessary, at least the way you have the code now, so you can remove
MyException ();
2) It's recommended to derive exceptions from std::exception.
3) You can catch your exceptions by catching a MyException&, and print the message there :
try
{
PersonValidator validator(Person());
}
catch(const MyException& ex)
{
std::cout << ex.getMessage();
}
4) Avoid using directives in headers. Your syntax suggests you have a using namespace std; in the header. That's wrong, you should favor full name qualification, at least in headers:
protected: std::string message;
MyException (std::string mes)
etc.
5) Favor pass by const reference instead of pass by value, for complex types:
MyException (const std::string& mes)
PersonValidator (const Person& p)
6) Aim for const correctness:
std::string getMessage()
should be:
std::string getMessage() const
since it doesn't change any members.
7) Use initialization lists:
MyException (string mes) {
this->message = mes;
}
becomes
MyException (string mes) : message(mes) {
}
you can also use default constructor to initialize to some pre-defined value.
MyException () : message ("throwing an exception") {};