Meditation, The Art of Exploitation

Thinking? At last I have discovered it--thought; this alone is inseparable from me. I am, I exist--that is certain. But for how long? For as long as I am thinking. For it could be, that were I totally to cease from thinking, I should totally cease to exist....I am, then, in the strict sense only a thing that thinks.

Wednesday, May 16, 2007

C++ Creating custom exception class

The following code segment demonstrates how to create a C++ custom exception class. The important thing to note is that the base exception:what() function has the following signature:
const char * what() const throw()

Thus to override the base exception::what function, the custom exception class must declare the function signature properly otherwise, it becomes a case of function name hidding as explained in one of the previous C++ articles.

class Exception : public exception
Exception(string m="exception!") : msg(m) {}
~Exception() throw() {}
const char* what() const throw() { return msg.c_str(); }

string msg;

int main()
throw Exception();
catch(exception& e)
cout << e.what() << endl;

return 0;