// custom_exception2.cc #include #include #include #include using namespace std; class MyException: public std::exception { string Message; public: MyException(const char *msg):Message(msg) {} virtual const char* what() const throw() { return Message.c_str(); } ~MyException() throw() {} }; double SafeSqrt(double x) { if (x < 0.0) throw MyException("MyException: Tried to take sqrt of negative number"); return sqrt(x); } // run this to see what happens if exception is not caught int main() { double x = -4.0, y; y = SafeSqrt(x); cout << "The square root of " << x << " is " << y << endl; }