C++ examples for Statement:try catch
Creating an Exception Class
#include <iostream> #include <string> using namespace std; class Exception { public:/* w ww . jav a2 s . c o m*/ Exception(const string& msg) : msg_(msg) {} ~Exception() {} string getMessage() const {return(msg_);} private: string msg_; }; void f() { throw(Exception("Mr. Test")); } int main() { try { f(); } catch(Exception& e) { cout << "You threw an exception: " << e.getMessage() << endl; } }