I would like to throw an exception when my C++ methods encounter something weird and can’t recover. Is it OK to throw a std::string pointer?
Here’s what I was looking forward to doing:
void Foo::Bar() { if(!QueryPerformanceTimer(&m_baz)) { throw new std::string('it's the end of the world!'); } } void Foo::Caller() { try { this->Bar(); // should throw } catch(std::string *caught) { // not quite sure the syntax is OK here... std::cout << 'Got ' << caught << std::endl; } }
Yes.
std::exceptionis the base exception class in the C++ standard library. You may want to avoid using strings as exception classes because they themselves can throw an exception during use. If that happens, then where will you be?boost has an excellent document on good style for exceptions and error handling. It’s worth a read.